Java 25 (LTS) reference · String Methods
Java String isBlank() Method
Return true if the string is empty or contains only whitespace. isEmpty() is true only for "".
Java 25 (LTS) Since Java 11
Syntax
string.isBlank()
Return value
true or false.
Examples
public class Main {
public static void main(String[] args) {
System.out.println("".isBlank());
System.out.println(" ".isBlank());
System.out.println(" ".isEmpty());
System.out.println(" a ".isBlank());
}
}
Output
true true false false
Run this on your computer — see “Get Started” for setup.
public class Main {
public static void main(String[] args) {
String name = " ";
String display = name.isBlank() ? "Anonymous" : name.strip();
System.out.println(display);
}
}
Output
Anonymous
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 11 | Added |
Related
string.strip()· Remove leading and trailing whitespace, including Unicode spaces. Prefer it to the older trim().string.length()· Return the number of characters (UTF-16 code units) in a string. An empty string has length 0.- Java Strings · tutorial