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.
1 2 3 4 5 6 7 8 |
String s = "abc"; System.out.println(s.isBlank()); s = ""; System.out.println(s.isBlank()); s = "t t"; System.out.println(s.isBlank()); |
Output:
1 2 3 4 5 |
<span style="color: #008000;"><strong><code> false true true </code></strong></span> |
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.
-
- lines(): This method returns a stream of lines extracted from the string, separated by line terminators such as n, r etc.
1 2 3 4 5 6 |
String s1 = "HinHellorWassup"; 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.
-
- 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.
- strip(), stripLeading(), stripTrailing(): These methods are used to strip whitespaces from the string. As the name suggests,
1 2 3 4 5 6 7 |
String s2 = " Hello, tWorldt "; System.out.println("https://www.journaldev.com/26288/#" + s2 + "https://www.journaldev.com/26288/#"); System.out.println("https://www.journaldev.com/26288/#" + s2.strip() + "https://www.journaldev.com/26288/#"); System.out.println("https://www.journaldev.com/26288/#" + s2.stripLeading() + "https://www.journaldev.com/26288/#"); System.out.println("https://www.journaldev.com/26288/#" + s2.stripTrailing() + "https://www.journaldev.com/26288/#"); |
Output:
1 2 3 4 5 6 |
# Hello, World # #Hello, World# #Hello, World # # Hello, World# <img class="alignnone wp-image-30270 size-full" src="https://all-learning.com/wp-content/uploads/2019/02/java-string-strip-stripLeading-stripTrailings.png" alt="java-string-strip-stripLeading-stripTrailing" width="1200" height="628" /> |
Java String strip(), stripLeading(), stripTrailing()
-
- repeat(int n): This method returns a new string whose value is the concatenation of this string repeated ‘n’ times.
1 2 3 4 5 6 |
String s3 = "Hellon"; 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