Modern String Methods in Java
For years, simple jobs such as "is this input only spaces?" or "draw a line of 40 dashes" needed a loop or an extra library. Java 11 and later versions added small methods to String that do these jobs in one call. They're worth learning because they make code shorter and handle tricky cases, like international spaces, that the older methods miss.
The New Methods in Action
public class Modern {
public static void main(String[] args) {
System.out.println("".isBlank() + " " + " \t".isBlank()
+ " " + " a ".isBlank());
System.out.println("[" + " x ".strip() + "|"
+ " x ".stripLeading() + "|" + " x ".stripTrailing() + "]");
System.out.println("-".repeat(20));
System.out.println("line1\nline2\r\nline3".lines().toList());
System.out.println("Hi %s, total Rs. %d".formatted("Srihitha", 450));
}
}true true false
[x|x | x]
--------------------
[line1, line2, line3]
Hi Srihitha, total Rs. 450How it works
isBlank()(Java 11) istruefor empty text and text made only of spaces, tabs or newlines.isEmpty()is only true for"".strip(),stripLeading()andstripTrailing()(Java 11) remove white space at both ends, the start only, or the end only.repeat(n)(Java 11) repeats the textntimes; perfect for separator lines.lines()(Java 11) splits text into lines, understanding\n,\r\nand\r, and returns a stream (turned into a list here).formatted(...)(Java 15) isString.formatwritten as a method on the template.
Why strip Is Better Than trim
trim() dates from Java 1.0 and only removes characters with codes up to 32 (the plain ASCII space and control characters). Text copied from websites or documents often contains other Unicode spaces, such as the "em space" ( ), which trim leaves behind:
public class Spaces {
public static void main(String[] args) {
String fromWeb = " Advitha ";
System.out.println(fromWeb.trim().length());
System.out.println(fromWeb.strip().length());
}
}9
7trim removed nothing, so the length is still 9; strip removed both Unicode spaces, leaving the 7 letters of "Advitha". Comparing user input with trim could make "Advitha" not equal to "Advitha".
Quick Reference
| Method | Since | What it does |
|---|---|---|
isBlank() | Java 11 | True if empty or only white space |
strip(), stripLeading(), stripTrailing() | Java 11 | Remove Unicode white space from the ends |
repeat(n) | Java 11 | The text repeated n times |
lines() | Java 11 | A stream of lines |
indent(n) | Java 12 | Add (or remove, if negative) n spaces at the start of each line |
transform(f) | Java 12 | Apply a function to the string: s.transform(String::strip) |
formatted(args) | Java 15 | Same as String.format(this, args) |
translateEscapes() | Java 15 | Turn \t, \n written as text into real tabs and newlines |
stripIndent() | Java 15 | Remove common leading spaces from every line, as text blocks do |
Common Mistakes
Using isEmpty() to reject blank input. " " is not empty; use isBlank().
Mixing trim and strip in one project. Pick strip for consistency.
Forgetting lines() returns a stream. Call .toList() or .count() on it.
Practice
- Easy: Print a receipt header: 30 equals signs, the shop name, then 30 equals signs, using
repeat. - Harder: Given a multi-line string of names, some blank lines and some with extra spaces, print each non-blank name stripped and numbered, using
lines(),strip()andisBlank().
Key Takeaways
Was this tutorial helpful?