Open In App

Java String isBlank() Method

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

The String.isBlank() method is introduced in Java 11 and it checks if a string is empty or contains only whitespace characters (such as spaces, tabs, or newlines). This method simplifies string validation.

Example 1: In this example, we will use the basic approach to check if a given string is blank (empty or contains only whitespace) using isBlank() method.

Java
// Java program to demonstrate isBlank() method public class StringIsBlank {    public static void main(String[] args) {    // Declare and initialize strings  String s1 = " "; // String with only spaces  String s2 = "Hello, World!"; // Non-empty string  // Check if the strings are blank  System.out.println(s1.isBlank());   System.out.println(s2.isBlank());   } } 

Output
true false 

Syntax of isBlank() Method

boolean isBlank()

Return Type:

  • Returns true, if the string is empty or only contains whitespace characters.
  • Returns false, if the string contains non-whitespace characters.

Example 2: This example demonstrates how to distinguish between an empty string ("") and a string that contains only whitespace characters.

Java
// Java program to distinguish between  // empty and blank strings public class StringIsBlank {  public static void main(String[] args) {    // Array of strings  String[] s = {"", " ", "Hello", " \t\n"};   // Loop through the array of strings  for (int i = 0; i < s.length; i++) {    // Check if the string is blank  if (s[i].isBlank()) {    System.out.println("String at index " + i   + " is blank (contains only whitespace).");  } else {  System.out.println("String at index " + i   + " is not blank.");  }  }  } } 

Output
String at index 0 is blank (contains only whitespace). String at index 1 is blank (contains only whitespace). String at index 2 is not blank. String at index 3 is blank (contains only whitespace). 

Example 3: This example shows how to use isBlank() with conditional logic to handle blank inputs in an application.

Java
// Java program to use isBlank() with conditional logic public class StringIsBlank {  public static void main(String[] args) {    // String with spaces  String s = " ";   // Check if the message is blank  if (s.isBlank()) {  System.out.println("The message is blank.");  } else {  System.out.println("Message: " + s);  }  } } 

Output
The message is blank. 

Comparing isBlank() with Other Methods

Before Java 11, developers often used isEmpty() or a combination of trim() and isEmpty() to check for blank strings. However, isBlank() provides a simpler and more efficient solution, as it automatically handles both empty and whitespace-only strings in one call.

  • isEmpty(): Checks if the string length is 0, but does not account for strings that contain only whitespace.
  • trim().isEmpty(): Removes leading and trailing whitespace and then checks if the resulting string is empty.
  • isBlank(): Checks if the string is empty or consists only of whitespace.

Example:

Java
public class CompareMethods {    public static void main(String[] args) {    // Only whitespace  String s = " ";   // Using isEmpty  System.out.println(s.isEmpty());   // Using trim() + isEmpty  System.out.println(s.trim().isEmpty());   // Using isBlank  System.out.println(s.isBlank());   } } 

Output
false true true 

Article Tags :

Explore