Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

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
false

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.