Java String isBlank() – Check Blank or Empty String

Learn to use String.isBlank() method to determine if a given string is blank or empty or contains only white spaces. The isBlank() method has been added in Java 11.

To check if a given string does not have even blank spaces, use String.isEmpty() method.

1. String isBlank() API

  • It returns true if the given string is empty or contains only white space code points, otherwise false.
  • It uses Character.isWhitespace(char) method to determine a white space character.
/**
* returns 	- true if the string is empty or contains only white space codepoints
*			- otherwise false
*/

public boolean isBlank()

2. String isBlank() Example

Java program to check if a given string is blank or not.

"ABC".isBlank()          //false

" ABC ".isBlank()        //false

"  ".isBlank()           //true

"".isBlank() )            //true

3. Difference between isBlank() and isEmpty()

Both methods are used to check for blank or empty strings in java. The difference between both methods is that:

  • isEmpty() method returns true if, and only if, the string length is 0.
  • isBlank() method only checks for non-whitespace characters. It does not check the string length.

In other words, a String with only white space characters is blank but not empty.

"ABC".isBlank()      //false
"  ".isBlank()       //true

"ABC".isEmpty()      //false
"  ".isEmpty()       //false

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode