StringUtils isEmpty() Vs isBlank()

StringUtils isEmpty() Vs isBlank() | Both isEmpty() and isBlank() are present in StringUtils class, these methods are used to find whether the string is empty or not. Even though they perform the same task but are some differences between them. In this blog, we will discuss the differences between StringUtils isEmpty() Vs isBlank() method.

StringUtils isBlank() vs isEmpty() Method Signature

The main difference:- The isBlank() method returns true if the string contains only whitespaces but the isEmpty() method returns false.

The syntax for both the methods is as follows:-
public static boolean isEmpty(final CharSequence cs)
public static boolean isBlank(final CharSequence cs)

Method call Example for isBlank() method,

 StringUtils.isBlank("")true
 StringUtils.isBlank(" ")true
 StringUtils.isBlank(null)true 
StringUtils.isBlank(“people”)false
 StringUtils.isBlank(”  string  “)false

Method call Example for isEmpty() method,

 StringUtils.isEmpty(""true
 StringUtils.isEmpty(" ")false
 StringUtils.isEmpty(null)true
StringUtils.isEmpty(“people”)false
 StringUtils.isEmpty(”  string  “)false

StringUtils isEmpty() Vs isBlank() Examples

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "    \n\t";
        System.out.println("String: " + string);
        System.out.println("String length: " + string.length());

        System.out.println(StringUtils.isEmpty(string));
        System.out.println(StringUtils.isBlank(string));
    }
}

Output:-

String:

String length: 6
false
true

The string literal contains some spaces, new line (‘/n’) and new tab (‘\t’) characters. It has a length greater than 0 therefore the StringUtils.isEmpty() method returns false. Since string contains only whitespaces, and after removing whitespaces string length becomes 0 therefore the StringUtils.isBlank() method returns true.

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "           ";
        System.out.println("String: " + string);
        System.out.println("String length: " + string.length());

        System.out.println(StringUtils.isEmpty(string));
        System.out.println(StringUtils.isBlank(string));
    }
}

Output:-

String:
String length: 11
false
true

Similar to the previous example, here also string contains only whitespaces therefore the StringUtils.isEmpty() returns false whereas the StringUtils.isBlank() returns true.

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "Know Program";
        System.out.println("String: " + string);
        System.out.println("String length: " + string.length());

        System.out.println(StringUtils.isEmpty(string));
        System.out.println(StringUtils.isBlank(string));
    }
}

Output:-

String: Know Program
String length: 12
false
false

In the above program, the string literal contains many characters and has a length greater than 0 therefore both StringUtils.isEmpty() and StringUtils.isBlank() method returns false.

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "      Hi    ";
        System.out.println("String length: " + string.length());

        System.out.println(StringUtils.isEmpty(string));
        System.out.println(StringUtils.isBlank(string));
    }
}

Output:-

String length: 12
false
false

In the above program, string literal contains space and characters. After excluding the whitespaces, the length of string literal is greater than 0 therefore both StringUtils.isEmpty() and StringUtils.isBlank() method returns false.

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.