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.
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;
public static boolean isAllBlank(final CharSequence... css)
CharSequence css
: Character sequence to check.The function returns true
if the character sequence is empty, null
, or contains whitespace only. Otherwise, it returns false
.
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));}}
""
Since the sequence is empty, the function returns true
.
"hello"
Since the sequence has characters, the function returns false
.
"\n\t"
Since the sequence has whitespace characters, i.e., newline and tab characters, the function returns true
.
null
Since the sequence is null
, the function returns true
.
The output of the code will be as follows:
true
false
true
true