StringUtils isBlank() Method

StringUtils isBlank() Method of Apache Commons Lang package | In this blog, we will discuss the isBlank() method of the StringUtils class of Apache Commons Lang package. The isBlank() method is used to check whether the given string is blank or not. The given string is blank when it has zero characters, when the string points to the null, or when the string contains the whitespace characters.

Java StringUtils isBlank() Method

The syntax of the isBlank() method is as follows:-
public static boolean isBlank(final CharSequence cs)
It checks if a CharSequence is empty (""), null or whitespace only.

Parameters: It accepts one parameter i.e. the string to check whether it is a bank or not.
Return type: boolean – true or false
Returns: true if the string is blank or else false.

Method CallResult
StringUtils.isBlank(null)true
StringUtils.isBlank("")true
StringUtils.isBlank(" ")true
StringUtils.isBlank("KP")false
StringUtils.isBlank(" KP ")false

StringUtils isBlank() Method Example-1

Let us see demonstrations of the StringUtils.isBlank() method of the Apache Commons Lang package.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "KnowProgram";
        System.out.println(StringUtils.isBlank(string));

        String string1 = "";
        System.out.println(StringUtils.isBlank(string1));
    }
}

Output:-

false
true

The string “KnowProgram” has a length greater than 0 therefore isBlank() method return false, whereas the variable string1 holds an empty string literal therefore StringUtils.isBlank(string1) returns true.

StringUtils.isBlank() Method Example-2

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = null;
        System.out.println(StringUtils.isBlank(string));

        String string1 = "   ";
        System.out.println(StringUtils.isBlank(string1));
    }
}

Output:-

true
true

When the string is pointing to null, in that case, the isBlank() method of StringUtils class always returns true. Similarly, when the string contains only white space then also it returns true. The StringUtils.isBlank() method ignores all the whitespaces and hence string with only whitespace becomes empty and it returns true.

Java StringUtils isBlank() Method Example-3

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "KP";
        System.out.println(StringUtils.isBlank(string));

        String string1 = "  KP  ";
        System.out.println(StringUtils.isBlank(string1));
    }
}

Output:

false
false

The string literal “KP” has a length greater than 0 excluding the whitespaces therefore StringUtils.isBlank() method returns false. Similarly for the variable string1 where after excluding the whitespaces length of the string is greater than 0 and hence StringUtils isBlank() method returns true.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *