Java 11 doesn’t have a lot of language specific features. So, it was surprising to see 6 new methods getting introduced in Java String Class.
Java 11 String Class New Methods
Let’s look at these new String class methods one by one.
- isBlank(): This method returns true if the string is empty or contains only white spaces code points.
- lines(): This method returns a stream of lines extracted from the string, separated by line terminators such as \n, \r etc.
- strip(), stripLeading(), stripTrailing(): These methods are used to strip whitespaces from the string. As the name suggests,
strip()
will remove leading and trailing whitespaces. However,stripLeading()
will remove only leading whitespaces andstripTrailing()
will remove only trailing whitespaces. - repeat(int n): This method returns a new string whose value is the concatenation of this string repeated ‘n’ times.
String s = "abc";
System.out.println(s.isBlank());
s = "";
System.out.println(s.isBlank());
s = "\t \t";
System.out.println(s.isBlank());
Output:
false
true
true
Notice that “\t” is considered as a white space character codepoint in Unicode.

Java String IsBlank()
I am using jShell to execute the code snippets without actually creating a java file.
String s1 = "Hi\nHello\rWassup";
System.out.println(s1);
List lines = s1.lines().collect(Collectors.toList());
System.out.println(lines);
Output:

Java String lines() Function
This method is useful to process multi-line strings with one line at a time.
String s2 = " Hello, \tWorld\t ";
System.out.println("#" + s2 + "#");
System.out.println("#" + s2.strip() + "#");
System.out.println("#" + s2.stripLeading() + "#");
System.out.println("#" + s2.stripTrailing() + "#");
Output:
# Hello, World #
#Hello, World#
#Hello, World #
# Hello, World#

Java String strip(), stripLeading(), stripTrailing()
String s3 = "Hello\n";
System.out.println(s3.repeat(3));
s3 = "Do";
System.out.println(s3.repeat(2));
Output:

Java String repeat()
Conclusion
Java String class has a lot of utility methods. However, all these new utility methods will be very useful because we won’t have to worry about writing them ourselves and thinking about whether they cover all the rare scenarios related to different types of Unicode characters or not.
Reference: Java 11 String Class API Doc
Pankaj, Can you create a new link on the side panel for Java 12 features. It is hard to find for a quick navigation.
awesome! Thanks for introduction
Awesome, Keep good work!!!!!!!
How do the Strip methods different from trim ones?
If you look at the trim() method documentation, it removes all leading and trailing spaces, where space is defined as any character whose codepoint is less than or equal to ‘U+0020’ (the space character).
Whereas, strip() is using Character.isWhitespace() function to identify the white space and remove it.
There might be a scenario where a whitespace character codepoint is greater than ‘U+0020’, then trim() will not remove it whereas strip() will remove it.
I would suggest using strip() function rather than trim() to remove white spaces going forward.
Hey Pankaj,
Really appreciate what you do.
This is the perfect place for learning about new features of latest Java release and much more.
Kudos and Respect.
Thanks for the kind words, they push me to do even better in the future.