Java 11 — String API enhancements
Java 11 brought a new methods in the String class which streamlined few aspects of using third party libraries for string operations and checks. Below are few methods introduced in String Class
isBlank()lines()strip()stripLeading()stripTrailing()repeat()
Lets dive into details of
isBlank()
we already have isEmpty() introduces in Java 6 in String class and why do we need isBlank() which has been now introduced in the Java 11. Let have a look at the isEmpty()
Logic: " ".isEmpty() → false
the above logic returns false but technically it is empty string , but it is not a empty string as it has space character so isEmpty() check the string length , as the empty characters holds Unicode so it returns length >0 and states the result as false
To overcome this issue/s pertaining to the special characters (e.g., " ", "\t", "\n") we now have an another String API method isBlank() returns true nullifying the characters unicode.
public static void main(String[] args) {
String str = "";
boolean result = str.isEmpty();
System.out.println("result isEmpty:"+result); //result isEmpty:true
String str1 = " \t";
boolean result1 = str1.isEmpty();
System.out.println("result isEmpty:"+result1); //result isEmpty:false
}- Logic:
"".isBlank()→true - Logic:
" ".isBlank()→true isEmpty()is for checking if a string has zero length.isBlank()goes further to check not only for zero length but also if the string consists solely of whitespace characters.
Lets dive into details of lines
()
This returns a Stream of lines extracted from the string, separated by line terminators (like \n or \r)
String multiline = "Line 1\nLine 2\nLine 3";
multiline.lines().forEach(System.out::println);In a real world scenario’s this is very helpful when parsing an text file or csv file with many lines separated at the end with \r or \n. To read the lines and make it array of lines(string)s this lines method is way to go method and easy way to intercept all the lines in a file.
Get Vamsi balaga’s stories in your inbox
Join Medium for free to get updates from this writer.
According to the Java documentation, it recognizes:
\n(Line Feed)\r(Carriage Return)\r\n(Carriage Return + Line Feed)
Key Benefits : Lazy loading and memory efficient. The reason lines() is special is that it is lazy. It doesn't split the whole string into memory at once like split() does. It reads the string as you consume the stream, which is much better for performance when dealing with massive blocks of text
Lets dive into details of
strip()
This is the “modern” version of trim().
trim(): Only removes characters with Unicode less than or equal toU+0020(standard space).strip(): Is Unicode-aware. It removes all characters that the Unicode standard defines as white space.
Note: You also have stripLeading() and stripTrailing() for specific sides
Lets dive into details of
repeat()
repeat(int n) simple and effective for repetitive strings to print n times as defined. It returns a string whose value is the concatenation of the original string repeated n times.
String stars = "*".repeat(5); // Result: "*****"Happy reading and exploring! 🚀✨ Thank you !!







