Java 11 String methods| lines strip isBlank examples| new features

In this blog post, learn the six new methods added to the String class in Java 11.

JDK 11 - Strings methods

Java 11 Version added a few more methods to the string class. This method helps the developer to simplify the coding styles and improve performance.

lines() example

Returns the Stream of strings separated with a line break from multi-line strings
Syntax:

 Stream<String> lines()
        Stream lines = string.lines();

This method can be rewritten in the older version using the stream below.

String linesExample= string.lines()
.map(String::trim)
.collect(joining("\n"));

Example:

lines() method is not Unicode aware compliant


String multilinesstring="String\\nlines\\ndemo\\n";
multilinesstring.lines().forEach(System.out::println); // returns String lines demo

strip() example

This method removes Unicode whitespace characters from a string.

It has the same behavior as a trim() method.

strip() method is not Unicode aware compliant.

System.out.println(" cloud".strip()); // prints "cloud"
System.out.println("hadoop ".strip()); // prints "hadoop"
System.out.println(" cloudhadoop ".strip()); // prints "cloudhadoop"

stringLeading() method

This method removes a leading whitespace character from String.

stringTrailing() example

This method removes a trailing whitespace character from String.

stringTrailing+stringLeading is equal to strip() method.

isBlank() example

This method is used to check if the string is empty or only contains whitespace characters

repeat(int) example

return the number of times the string wants to repeat. number of times can be configured as a parameter to this method

Conclusion

In this tutorial, Learned new methods introduced in the java11 version with examples.