Check for blank strings with a single method call.
Porównanie kodu
✕ Java 8
boolean blank =
str.trim().isEmpty();
// or: str.trim().length() == 0
✓ Java 11+
boolean blank = str.isBlank(); // handles Unicode whitespace too
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
trim().isEmpty()
Nowoczesne podejście
isBlank()
Od JDK
11
Poziom trudności
Początkujący
Wsparcie JDK
String.isBlank()
Dostępne
Widely available since JDK 11 (Sept 2018)
Jak to działa
isBlank() returns true if the string is empty or contains only whitespace, including Unicode whitespace characters that trim() misses.
Powiązana dokumentacja