What is StringUtils.isAllBlank in Java?

isAllBlank is a static method of the StringUtils class that checks whether a given string is empty, null, or only contains whitespace characters.


A character is classified as whitespace using Character. isWhitespace.

How to import StringUtils

StringUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

Note: For other versions of the commons-lang package, refer to the Maven Repository.

You can import the StringUtils class as follows:

import org.apache.commons.lang3.StringUtils;

Syntax


public static boolean isAllBlank(final CharSequence... css)

Parameters

  • CharSequence css: Character sequence to check.

Return value

The function returns true if the character sequence is empty, null, or contains whitespace only. Otherwise, it returns false.

Code

import org.apache.commons.lang3.StringUtils;
public class Main{
public static void main(String[] args){
String characterSequence = "";
System.out.println(StringUtils.isAllBlank(characterSequence));
characterSequence = "hello";
System.out.println(StringUtils.isAllBlank(characterSequence));
characterSequence = "\n\t";
System.out.println(StringUtils.isAllBlank(characterSequence));
System.out.println(StringUtils.isAllBlank(null));
}
}

Explanation

  1. character sequence = ""

Since the sequence is empty, the function returns true.

  1. character sequence = "hello"

Since the sequence has characters, the function returns false.

  1. character sequence = "\n\t"

Since the sequence has whitespace characters, i.e., newline and tab characters, the function returns true.

  1. character sequence = null

Since the sequence is null, the function returns true.

Output

The output of the code will be as follows:


true
false
true
true