String.isBlank()
Checks whether a string is empty or contains only whitespace.
Available since: Java 11
Syntax
public boolean isBlank()Return value
booleantrue if the string has nothing but whitespace.
Examples
Compared with isEmpty():
void main() {
IO.println("".isBlank());
IO.println(" ".isBlank());
IO.println(" ".isEmpty());
IO.println(" a ".isBlank());
}Output:
true
true
false
falseNotes
For checking user input isBlank() usually fits better than isEmpty(): a string of spaces also counts as "nothing entered". It cannot be called on null — you get a NullPointerException.
See also
- String.isEmpty() — Checks whether the string length is zero.
- String.strip() — Removes any Unicode whitespace from both ends of a string.
Related recipes
In the tutorial
- String methods — The main String methods in Java: searching, substring, changing case, strip, replace, split, join, repeat and string checks.