Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, May 8, 2019

Java 11 String API New Methods

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.

No comments:

Post a Comment

Please do not add any spam links in the comments section.