Java – Check if a String is Blank

A string is said to be blank if it is empty or contains only white space characters like single space, new line, return feed, tab space, etc.

To check if a String str is blank, use String.isBlank() method. Call isBlank() method on the string str. If str is blank, then isBlank() method returns true.

Note: We can use String.isBlank() method only from Java version 11.

Java Program

public class Example {
	public static void main(String[] args) {
		String str = "\t  \n \r";
		boolean result = str.isBlank();
		System.out.println("Is our string blank? " + result);
	}
}

Output

Is our string blank? true

Empty string is a blank string.

Java Program

public class Example {
	public static void main(String[] args) {
		String str = "";
		boolean result = str.isBlank();
		System.out.println("Is our string blank? " + result);
	}
}

Output

Is our string blank? true

If str contains any character other than white space characters, then isBlank() method returns false.

Java Program

public class Example {
	public static void main(String[] args) {
		String str = "hello";
		boolean result = str.isBlank();
		System.out.println("Is our string blank? " + result);
	}
}

Output

Is our string blank? false

Conclusion

In this Java Tutorial, we learned how to check if a string is blank in Java.