New String Methods in Java 11
Java 11 added six small but very useful methods to String: isBlank(), strip(), stripLeading(), stripTrailing(), lines() and repeat(int). They replace common helper code that projects used to write themselves or import from libraries.
This tutorial explains each method, the important difference between strip() and trim(), and a practical clean-up example.
1. Why Were These Methods Added?
Checking for whitespace-only input, splitting text into lines and padding text are everyday tasks. Before Java 11 they needed regular expressions, loops or libraries such as Apache Commons. trim() also had a hidden limitation: it only understands ASCII whitespace.
2. Method Summary
| Method | Returns | Description |
|---|---|---|
isBlank() | boolean | true if empty or only whitespace |
strip() | String | removes leading and trailing whitespace (Unicode-aware) |
stripLeading() / stripTrailing() | String | removes whitespace at one end only |
lines() | Stream<String> | splits on \n, \r or \r\n |
repeat(int count) | String | the string repeated count times |
3. Practical Examples
Example 1: Each method in action
The second string starts and ends with the Unicode em space (U+2003). trim() keeps it, while strip() removes it.
public class NewStringMethods {
public static void main(String[] args) {
String spaces = " ";
String padded = " Java 11 "; // contains Unicode em spaces
// isBlank(): true for empty or whitespace-only strings
System.out.println("isEmpty : " + spaces.isEmpty() + ", isBlank: " + spaces.isBlank());
// strip() is Unicode-aware; trim() only removes characters <= U+0020
System.out.println("trim() : [" + padded.trim() + "]");
System.out.println("strip() : [" + padded.strip() + "]");
System.out.println("leading : [" + " left".stripLeading() + "], trailing: [" + "right ".stripTrailing() + "]");
// repeat(n)
System.out.println("repeat : " + "=-".repeat(6));
// lines(): a Stream of lines, handling \n, \r and \r\n
String report = "id,item\r\n1,pen\n2,ink";
report.lines().map(line -> " > " + line).forEach(System.out::println);
System.out.println("line count: " + report.lines().count());
}
}
isEmpty : false, isBlank: true trim() : [ Java 11 ] strip() : [Java 11] leading : [left], trailing: [right] repeat : =-=-=-=-=-=- > id,item > 1,pen > 2,ink line count: 3
Example 2: Cleaning pasted input
lines(), strip() and isBlank() work well together in a stream, and repeat() builds simple text layouts.
import java.util.List;
import java.util.stream.Collectors;
public class CleanInput {
public static void main(String[] args) {
// A pasted block of tags with blank lines and stray spaces
String pasted = " java \n\n spring\t\n \n kafka \n";
List<String> tags = pasted.lines()
.map(String::strip)
.filter(line -> !line.isBlank())
.collect(Collectors.toList());
System.out.println("Tags: " + tags);
// Build a simple text table with repeat()
String border = "+" + "-".repeat(12) + "+";
System.out.println(border);
for (String tag : tags) {
System.out.println("| " + tag + " ".repeat(11 - tag.length()) + "|");
}
System.out.println(border);
}
}
Tags: [java, spring, kafka] +------------+ | java | | spring | | kafka | +------------+
4. strip() vs trim()
5. Important Points and Best Practices
- Prefer
strip()overtrim()for text that may contain Unicode whitespace. isBlank()is true for""as well;isEmpty()is true only for"".repeat(0)returns an empty string; a negative count throwsIllegalArgumentException.lines()is lazy and handles Windows (\r\n) and Unix (\n) line endings.- Calling these methods on
nullstill throwsNullPointerException.
6. Summary
- Java 11 added
isBlank,strip,stripLeading,stripTrailing,linesandrepeat. strip()is Unicode-aware;trim()only removes ASCII control characters and spaces.lines()returns a lazy stream of lines.repeat()replaces manual loops for building repeated text.
Related Tutorials
7. References
Frequently Asked Questions (FAQ)
Q1. What is the difference between trim() and strip()?
trim() removes characters with code points up to U+0020. strip() removes all characters for which Character.isWhitespace returns true, including Unicode spaces.
Q2. What is the difference between isEmpty() and isBlank()?
isEmpty() is true only for a string of length 0. isBlank() is also true when the string contains only whitespace.
Q3. What does String.lines() return?
A Stream<String> of the lines in the string, split on \n, \r or \r\n, without the line terminators.
Q4. Which Java version added String.repeat()?
Java 11. "ab".repeat(3) returns "ababab".