Strings Beginner

Check for blank strings with a single method call.

โœ• Java 8
boolean blank =
    str.trim().isEmpty();
// or: str.trim().length() == 0
โœ“ Java 11+
boolean blank = str.isBlank();
// handles Unicode whitespace too
See a problem with this code? Let us know.
๐Ÿ“–

Self-documenting

isBlank() says exactly what it checks.

๐ŸŒ

Unicode-aware

Handles all Unicode whitespace, not just ASCII.

โšก

No allocation

No intermediate trimmed string is created.

Old Approach
trim().isEmpty()
Modern Approach
isBlank()
Since JDK
11
Difficulty
Beginner
String.isBlank()
Available

Widely available since JDK 11 (Sept 2018)

isBlank() returns true if the string is empty or contains only whitespace, including Unicode whitespace characters that trim() misses.

Share ๐• ๐Ÿฆ‹ in โฌก