Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
string
java

What is String.isBlank in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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());
    }
}

RELATED TAGS

communitycreator
string
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring