What is String.isBlank in Java?

Overview

isBlank() is an instance method that returns true if the string is empty or contains only white space codepoints. This method was introduced in Java 11.

If the string contains only white spaces, then applying this method will return true.

To summarize, the method returns true if it has no characters or only white spaces in it. Otherwise, it returns false, meaning characters other than white spaces are present in the string.

Example

In the code below, we define three different strings. One is an empty string, one has only white spaces, and one has characters. The output of the code when run would be as follows:

"" is blank - true  
"   " is blank - true  
"h i  " is blank - false

Code:

public class Main {

    public static void main(String[] args) {
        String s = "";
        System.out.println("\"" + s + "\" is blank - " + s.isBlank());
        s = "   ";
        System.out.println("\"" + s + "\" is blank - " + s.isBlank());
        s = "h i  ";
        System.out.println("\"" + s + "\" is blank - " + s.isBlank());
    }
}