Java - String isBlank() method



The Java String isBlank() method is, used to determine whether a string is blank or not. A blank string is a string that contains one or more white spaces. It returns true if and only if, the string is empty or contains the white-spaces; false otherwise.

In Java, the isBlank() and isEmpty() methods return the same values, but the difference is the isBlank() method considers that the white-spaces are blank strings. Whereas, the isEmpty() method considers the number of white-spaces as the string length.

The isBlank() method does not accept any parameter. It throws an exception if the given string is null.

Syntax

Following is the syntax of the Java String isBlank() method −

public boolean isBlank()

Parameters

  • It does not accept any parameter.

Return Value

This method returns true if the string is empty or contains only white space codepoints, otherwise false.

Example

If the given string is not null, the isBlank() method returns false.

In the following program, we are creating an object of the string class with the value TutorialsPoint. Then, using the isBlank() method, we are checking whether the string is black or not.

import java.lang.*;
public class TP {
   public static void main(String[] args){
      
      //create an object of the string 
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      boolean b = str.isBlank();
      System.out.println("The given string isBlank: " + b);
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: TutorialsPoint
The given string isBlank: false

Example

If the given string is empty, and contains only whitespaces then the isBlank() method returns true.

In this example, we are creating a string literal with some white spaces and an empty value. Using the isBlank() method, we are trying to check whether the current string value is blank or not.

import java.lang.*;
public class CheckBlank {
   public static void main(String[] args) {
      
      //create string literal
      String str = "   ";
      System.out.println("The given string is: " + str);
      System.out.println("The string length is: " + str.length());
      
      //using isBlank() method
      System.out.println("The string is blank or not? " + str.isBlank());
   }
}

Output

Following is the output of the above program −

The given string is:   
The string length is: 3
The string is blank or not? true

Example

Using conditional statements to check whether the given string is blank or not.

In this program, we are instantiating the string class with the value of TutorialsPoint. Then, using the isBlank() method, we are trying to check whether the string is blank or not.

import java.lang.*;
public class CheckBlank {
   public static void main(String[] args) {
      
      //instantiate the  string class
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      System.out.println("The string length is: " + str.length());
      
      //using isBlank() method
      boolean bool = str.isBlank();
      System.out.println("The isBlank() method returns: " + bool);
      if(bool) {
         System.out.println("The string is blank string.");
      } else {
         System.out.println("The string is not a blank string.");
      }
   }
}

Output

The above program, produces the following output −

The given string is: TutorialsPoint
The string length is: 14
The isBlank() method returns: false
The string is not a blank string.

Example

If the given string value is a null, this method throws a NullPointerException.

IIn the below example, we are creating a string literal with the null value. Using the isBlank() method, we are trying to check whether the string is blank or not. Since the string value is null, the method throws an exception.

import java.lang.*;
public class TP {
   public static void main(String[] args){
      try {
         
         //create a string literal with null value
         String str = null;
         System.out.println("The given string is: " + str);
         
         //using isBlank() method
         boolean b = str.isBlank();
         System.out.println("The null.isBlank() method returns: " + b);
      }catch(Exception e){
         e.printStackTrace();
      }
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: null
java.lang.NullPointerException: Cannot invoke "String.isBlank()" because "str" is null
   at TP.main(TP.java:9)
java_lang_string.htm
Advertisements