The String class in Java provides the methods strip(), isBlank(), and lines(), which were introduced in Java 11. These methods are useful for managing whitespaces, checking for blank strings, and processing multi-line strings.
1. strip()
The strip() method removes leading and trailing whitespaces from a string. Unlike trim(), it uses Unicode-aware whitespace handling, making it more robust for international characters.
Example:
public class StringStripExample {
public static void main(String[] args) {
String str = " \u2009Hello World "; // Unicode whitespace
System.out.println(str.strip()); // Outputs: "Hello World"
System.out.println(str.stripLeading()); // Removes leading spaces: "Hello World "
System.out.println(str.stripTrailing()); // Removes trailing spaces: " \u2009Hello World"
}
}
Key Point:
strip() differs from trim() in that it removes all Unicode whitespace, not just ASCII spaces.
2. isBlank()
The isBlank() method checks whether a string is empty or contains only whitespaces. This includes Unicode whitespace and helps to quickly validate string content.
Example:
public class StringIsBlankExample {
public static void main(String[] args) {
String empty = " "; // Contains whitespaces
System.out.println(empty.isBlank()); // Outputs: true
String nonBlank = "Hello";
System.out.println(nonBlank.isBlank()); // Outputs: false
String unicodeSpace = "\u2009"; // Unicode whitespace
System.out.println(unicodeSpace.isBlank()); // Outputs: true
}
}
Key Point:
isBlank() is stronger than isEmpty() because it treats strings with only whitespace as blank, whereas isEmpty() considers only an empty string ("").
3. lines()
The lines() method breaks a multi-line string into a stream of lines, using the platform’s line terminator (e.g., \n or \r\n) to split the string.
Example:
public class StringLinesExample {
public static void main(String[] args) {
String multiLineString = "Hello\nWorld\nJava 11";
// Use 'lines()' to split the multi-line string
multiLineString.lines().forEach(System.out::println);
// Output:
// Hello
// World
// Java 11
}
}
Key Points:
lines() splits the string into lines and returns a Stream<String>.
- It can be combined with stream operations like
filter(), map(), and forEach().
Combining Methods for Common Use Cases
Here’s how you can combine them:
Trim and Check Blank:
public class StringExample {
public static void main(String[] args) {
String input = " ";
if (input.strip().isBlank()) {
System.out.println("Input is blank or empty!");
} else {
System.out.println("Input: " + input.strip());
}
}
}
Processing Multi-Line Strings:
public class MultiLineExample {
public static void main(String[] args) {
String text = " Line 1 \n Line 2 \n Line 3 ";
text.lines()
.map(String::strip) // Clean up each line
.forEach(System.out::println);
// Output:
// Line 1
// Line 2
// Line 3
}
}
Summary of Functionalities:
strip(): Removes leading/trailing Unicode whitespace.
isBlank(): Checks if a string is empty or only whitespace.
lines(): Processes multi-line strings by splitting them into lines.