Java String isBlank() Method Last Updated : 09 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article 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()); } } Outputtrue false Syntax of isBlank() Methodboolean 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."); } } } } OutputString 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); } } } OutputThe message is blank. Comparing isBlank() with Other MethodsBefore 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()); } } Outputfalse true true Comment More infoAdvertise with us Next Article Java String contains() Method with Example J juhisrivastav Follow Improve Article Tags : Java Java-Strings Practice Tags : JavaJava-Strings Similar Reads Java StringUtils.isNoneBlank() Method with Examples The StringUtils.isNoneBlank() is a method provided by the Apache Commons Lang library for string manipulation in Java. It is used to check if any of the given strings are not blank or empty. The StringUtils.isNoneBlank() function is part of the Apache Commons Lang library, which provides utility met 3 min read Java String contains() Method with Example The String.contains() method is used to search for a sequence of characters within a string. In this article, we will learn how to effectively use the string contains functionality in Java.Example:In this example, we check if a specific substring is present in the given string.Java// Java Program to 3 min read Java String isEmpty() Method with Example In Java, the String isEmpty() method checks if a string is empty (length is zero). This method returns true if the string is empty and false otherwise. It is useful for validating strings in our applications.In this article, we will learn how to use the isEmpty() method in Java along with examples t 3 min read Stack empty() Method in Java The java.util.Stack.empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false. Syntax: STACK.empty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if th 4 min read Optional isPresent() Method in Java The isPresent() method of the java.util.Optional class is used to check if a value is present in the Optional object. If there is no value present in this Optional instance, then this method returns false, else true.Syntax of Optional isPresent() Methodpublic boolean isPresent()Parameters: This meth 2 min read Java Vector contains() Method The contains() method in Java is used to check whether a specific element is present in the Vector or not.Example 1: In this example, we will check whether a particular string is present in the vector or not.Java// Java program to demonstrate the use // of the contains() method with Strings import j 3 min read Like