Java String isBlank()

Introduction

The String.isBlank() method, introduced in Java 11, checks whether a string is empty or contains only whitespace characters (like spaces, tabs, or newlines). This method is useful when you want to verify if a string has meaningful content or is "blank" due to being empty or consisting solely of whitespace.

isBlank() differs from the older isEmpty() method, which only checks if the string's length is zero. The isBlank() method, on the other hand, treats strings with only whitespace as "blank."

Solution Steps

  1. Use isBlank() on Empty Strings: Check if an empty string is blank.
  2. Use isBlank() on Strings with Whitespace: Check if strings containing only whitespace characters are considered blank.
  3. Use isBlank() on Non-Empty Strings: Confirm that isBlank() returns false for strings with actual content.
  4. Compare isBlank() with isEmpty(): Understand the difference between isBlank() and isEmpty().

Java Program

Example 1: Basic Usage of isBlank()

public class StringIsBlankExample {
    public static void main(String[] args) {
        // Step 1: Check if an empty string is blank
        String emptyString = "";
        System.out.println("Is empty string blank? " + emptyString.isBlank());

        // Step 2: Check if a string with only spaces is blank
        String spacesOnly = "   ";
        System.out.println("Is string with spaces blank? " + spacesOnly.isBlank());

        // Step 3: Check if a non-empty string is blank
        String nonEmptyString = "Java";
        System.out.println("Is non-empty string blank? " + nonEmptyString.isBlank());
    }
}

Output

Is empty string blank? true
Is string with spaces blank? true
Is non-empty string blank? false

Explanation

  • Step 1: Empty String
    The isBlank() method returns true because an empty string is considered blank.
String emptyString = "";
System.out.println("Is empty string blank? " + emptyString.isBlank());  // true
  • Step 2: String with Spaces Only
    A string containing only spaces is also considered blank, so isBlank() returns true.
String spacesOnly = "   ";
System.out.println("Is string with spaces blank? " + spacesOnly.isBlank());  // true
  • Step 3: Non-Empty String
    A string with actual content, like "Java", is not blank, so isBlank() returns false.
String nonEmptyString = "Java";
System.out.println("Is non-empty string blank? " + nonEmptyString.isBlank());  // false

Example 2: Comparing isBlank() with isEmpty()

public class StringIsBlankVsIsEmpty {
    public static void main(String[] args) {
        // Step 4: Check difference between isBlank() and isEmpty()
        String onlySpaces = "   ";

        System.out.println("Is string with spaces empty? " + onlySpaces.isEmpty());  // false
        System.out.println("Is string with spaces blank? " + onlySpaces.isBlank());  // true

        String emptyString = "";
        System.out.println("Is empty string empty? " + emptyString.isEmpty());  // true
        System.out.println("Is empty string blank? " + emptyString.isBlank());  // true
    }
}

Output

Is string with spaces empty? false
Is string with spaces blank? true
Is empty string empty? true
Is empty string blank? true

Explanation

  • Step 4: Comparing isBlank() and isEmpty()
    The isEmpty() method checks whether a string's length is zero. A string that contains only spaces (or other whitespace characters) is not empty, but it is blank.
String onlySpaces = "   ";
System.out.println("Is string with spaces empty? " + onlySpaces.isEmpty());  // false
System.out.println("Is string with spaces blank? " + onlySpaces.isBlank());  // true

For an empty string, both isEmpty() and isBlank() return true:

String emptyString = "";
System.out.println("Is empty string empty? " + emptyString.isEmpty());  // true
System.out.println("Is empty string blank? " + emptyString.isBlank());  // true

Example 3: Handling Strings with Tabs and Newlines

public class StringIsBlankWithTabsNewlines {
    public static void main(String[] args) {
        // Step 5: Check strings with tabs and newlines
        String tabAndNewline = "\t\n";

        System.out.println("Is string with tab and newline blank? " + tabAndNewline.isBlank());  // true
    }
}

Output

Is string with tab and newline blank? true

Explanation

  • Step 5: Strings with Tabs and Newlines
    The isBlank() method treats whitespace characters such as tabs (\t) and newlines (\n) as blank, so it returns true for strings containing only those characters.
String tabAndNewline = "\t\n";
System.out.println("Is string with tab and newline blank? " + tabAndNewline.isBlank());  // true

Example 4: Using isBlank() in Conditional Statements

public class StringIsBlankConditional {
    public static void main(String[] args) {
        String userInput = "   ";  // Simulating user input with only spaces

        // Step 6: Using isBlank() to validate user input
        if (userInput.isBlank()) {
            System.out.println("User input is invalid (blank).");
        } else {
            System.out.println("User input is valid.");
        }
    }
}

Output

User input is invalid (blank).

Explanation

  • Step 6: Using isBlank() to Validate Input
    This example simulates user input where the string contains only spaces. The isBlank() method checks if the input is blank, and based on that, the program outputs whether the input is valid or invalid.
String userInput = "   ";  // User input with only spaces
if (userInput.isBlank()) {
    System.out.println("User input is invalid (blank).");
} else {
    System.out.println("User input is valid.");
}

In this case, the string is blank, so the program prints "User input is invalid (blank)."


Key Points

  • isBlank() vs isEmpty():

    • isBlank(): Returns true if the string is empty or contains only whitespace characters.
    • isEmpty(): Returns true only if the string is empty (i.e., its length is zero), but not if it contains spaces or other whitespace characters.
  • Whitespace Characters:
    isBlank() considers spaces, tabs (\t), newlines (\n), and carriage returns (\r) as blank characters.

  • Use Cases:
    isBlank() is useful when you want to validate if a string contains meaningful content (i.e., not just whitespace), such as when processing user input in forms.


Conclusion

The isBlank() method introduced in Java 11 is a helpful utility for determining if a string is either empty or consists only of whitespace characters. It goes beyond what isEmpty() offers by considering various whitespace characters as "blank." This method simplifies string validation, especially when handling user input or processing multi-line text where whitespace may occur.

Comments