$show=/label

Java 11 String API New Methods

SHARE:

Learn Java 11 New Methods in String Class. New methods of Java 11 String class are 1) isBlank() 2) lines() 3) repeat​(int count) 4) strip() 5) stripLeading() 6) stripTrailing(). Learn all new methods with example programs.

1. Java 11 String API New Methods Overview:

Java 11 is a new updated version with lots of changes similar to java 8. Java is free to use in production environments till java 10. But, java 11 onwards we should get license to use production environments.

String is a collection of characters including alphabets, numeric, special characters and as well supports 256 bit character set (any character).
Java 11 String API

Java strings are constants that means once created, values of string can not be changed. This is called as Immutable. Immutable is to get the advantage sharing the objects in multi-threading.

Following are the new methods added in Java 11 java.lang.String class.

Java 11 - String API New Methods

1.1 Java 11 String API Additions:

1) isBlank()
2) lines()
3) repeat​(int count)
4) strip()
5) stripLeading()
6) stripTrailing()

2. isBlank() :

As name of method it checks if the string is empty or it contains white space(s). If yes, it returns true, otherwise false.

2.1 Signature

public boolean isBlank()

2.2 Example

We will see few examples on isBlank() methods.

package examples.java.w3schools;

public class StringIsBlankExample {

 public static void main(String[] args) {

  // 1. Empty
  String java11Str1 = "";
  System.out.println("java11 string 1 is blank :: " + java11Str1.isBlank());
  
  //2. Single white space
  String java11Str2 = " ";
  System.out.println("java11 string 2 is blank :: " + java11Str2.isBlank());
  
  //3. Multiple white spaces
  String java11Str3 = "   ";
  System.out.println("java11 string 3 is blank :: " + java11Str3.isBlank());
  
  //4. With value
  String java11Str4 = "hello";
  System.out.println("java11 string 4 is blank :: " + java11Str4.isBlank());
 }
}

2.3 Output:

java11 string 1 is blank :: true
java11 string 2 is blank :: true
java11 string 3 is blank :: true
java11 string 4 is blank :: false

Returned true if the string is empty or single white space or multiple white space and false if it has some value.

In real time projects, we mostly check the code like below for whether the string is empty or blank. This is the very common utility methods.

 String java11Str5 = "world";
 if(!java11Str5.equals("") || java11Str5.trim().length() != 0) {
  System.out.println("String is non empty");
 }

Now we can replace the above code with the new isBlank() method.

3. lines()

lines() method returns Stream of String objects. First we will take a string "hello world" and see what would be the output.

3.1 Signature

public Stream lines()

3.2 Example

package examples.java.w3schools;

import java.util.stream.Stream;

public class StringLinesExample {

 public static void main(String[] args) {

  String content = "hello world";
  Stream lines = content.lines();
  lines.forEach(line -> System.out.println("line : " + line));

 }
}

3.3 Output

line : hello world

It has printed just "hello world" which is as in input string.

and will see the size of Stream lines.

System.out.println("Returned Stream size: " + lines.toArray().length);

Output: 

Returned Stream size: 1 

Now we will add '\n' to the input string.


String content = "hello\nworld";
Stream lines = content.lines(); 
lines.forEach(line -> System.out.println("line : " + line)); 

Output:

line : hello
line : world

System.out.println("Returned Stream size: " + lines.toArray().length);

Output:

 Returned Stream size: 2

lines method returns a stream of lines extracted from this string, separated by line terminators.
lines method treats as a separate string where '\n' or '\r' is present in the input.

It is valid to have a single string with both '\n' and '\r' as below.

 String content = "hello\njava\nworld";
 Stream lines = content.lines();
 lines.forEach(line -> System.out.println("line : " + line));

Output:

line : hello
line : java
line : world

4. strip():

Strip method does removes the white space with all leading and trailing space.

4.1 Signature

public String strip()

4.2 Example


package examples.java.w3schools;

public class StringStripExample {

 public static void main(String[] args) {

  String content = "   java-w3schools   ";
  String stripContent = content.strip();
  System.out.println("stripContent : " + stripContent);
 }
}

4.3 Output:

stripContent : java-w3schools

For better understanding, we will print length of string before and after strip.

     System.out.println("content content: " + content.length()); 
     System.out.println("stripContent content: " + stripContent.length());

Output:

content content: 20
stripContent content: 14

Total 6 white space characters were removed.

4.4 Does it removes the white space in between the strings?

String content = "java  w3schools";
content.strip();

Answer: No.   

5 strip()

strip() method removes all leading and trailing white space and returns a new string.

5.1 Signature

public String strip()

5.2 Example

String 11: Following strip() methods are discussed in depth in post "Java 11 String Strip methods".


strip()
stripLeading()
stripTrailing() 

6. repeat()

As the name suggests, the repeat() instance method repeats the string content.

It returns a string whose value is the concatenation of the string repeated n times, where n is passed as a parameter.

6.1 Signature

public String repeat​(int count)

Additionally, repeat() returns an empty string if the string is empty or the count is zero.

6.2 Example

String output = "Hola " + "la ".repeat(4);

6.3 Output

Hola la la la la

7. Conclusion


In this quick article, we explored the new String APIs in Java 11.

Code examples that are showed in this post are available on GitHub.

Post your questions in the comment section.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 11 String API New Methods
Java 11 String API New Methods
Learn Java 11 New Methods in String Class. New methods of Java 11 String class are 1) isBlank() 2) lines() 3) repeat​(int count) 4) strip() 5) stripLeading() 6) stripTrailing(). Learn all new methods with example programs.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0444KJsLRrhx1X3qtme8M5sQlpqJJzsSLghLWBIRuob9iiNz55QN5m-C7uhpqiI_3KuruvrRUnp-aekGW_9uCm37jrlx6viepqvaXFpwbVawUubjrbnN_bRDzRDYyFWEfBmsegL7QOVo/s400/Java+11+-+String+API+New+Methods.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0444KJsLRrhx1X3qtme8M5sQlpqJJzsSLghLWBIRuob9iiNz55QN5m-C7uhpqiI_3KuruvrRUnp-aekGW_9uCm37jrlx6viepqvaXFpwbVawUubjrbnN_bRDzRDYyFWEfBmsegL7QOVo/s72-c/Java+11+-+String+API+New+Methods.PNG
JavaProgramTo.com
https://www.javaprogramto.com/2019/02/java-11-strings.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2019/02/java-11-strings.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content