StringUtils.isBlank() vs String.isEmpty()

JavaStringString Utils

Java Problem Overview


I ran into some code that has the following:

String foo = getvalue("foo");
if (StringUtils.isBlank(foo))
    doStuff();
else
    doOtherStuff();

This appears to be functionally equivalent to the following:

String foo = getvalue("foo");
if (foo.isEmpty())
    doStuff();
else
    doOtherStuff();

Is a difference between the two (org.apache.commons.lang3.StringUtils.isBlank and java.lang.String.isEmpty)?

Java Solutions


Solution 1 - Java

StringUtils.isBlank() checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.

From the linked documentation:

>Checks if a String is whitespace, empty ("") or null.
> > StringUtils.isBlank(null)      = true > StringUtils.isBlank("")        = true
> StringUtils.isBlank(" ")       = true
> StringUtils.isBlank("bob")     = false
> StringUtils.isBlank("  bob  ") = false

For comparison StringUtils.isEmpty:

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true  
 StringUtils.isEmpty(" ")       = false  
 StringUtils.isEmpty("bob")     = false  
 StringUtils.isEmpty("  bob  ") = false

Warning: In java.lang.String.isBlank() and java.lang.String.isEmpty() work the same except they don't return true for null.

java.lang.String.isBlank() (since Java 11)

java.lang.String.isEmpty()

Solution 2 - Java

The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,

StringUtils.isBlank()

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true  
 StringUtils.isBlank(" ")       = true  
 StringUtils.isBlank("bob")     = false  
 StringUtils.isBlank("  bob  ") = false

StringUtils.isEmpty

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true  
 StringUtils.isEmpty(" ")       = false  
 StringUtils.isEmpty("bob")     = false  
 StringUtils.isEmpty("  bob  ") = false

Solution 3 - Java

StringUtils isEmpty = String isEmpty checks + checks for null.

StringUtils isBlank = StringUtils isEmpty checks + checks if the text contains only whitespace character(s).

Useful links for further investigation:

Solution 4 - Java

StringUtils.isBlank() will also check for null, whereas this:

String foo = getvalue("foo");
if (foo.isEmpty())

will throw a NullPointerException if foo is null.

Solution 5 - Java

StringUtils.isBlank also returns true for just whitespace:

> isBlank(String str) > > Checks if a String is whitespace, empty ("") or null.

Solution 6 - Java

StringUtils.isBlank(foo) will perform a null check for you. If you perform foo.isEmpty() and foo is null, you will raise a NullPointerException.

Solution 7 - Java

The only difference between isBlank() and isEmpty() is:

StringUtils.isBlank(" ")       = true //compared string value has space and considered as blank

StringUtils.isEmpty(" ")       = false //compared string value has space and not considered as empty

Solution 8 - Java

StringUtils.isBlank() returns true for blanks(just whitespaces)and for null String as well. Actually it trims the Char sequences and then performs check.

StringUtils.isEmpty() returns true when there is no charsequence in the String parameter or when String parameter is null. Difference is that isEmpty() returns false if String parameter contains just whiltespaces. It considers whitespaces as a state of being non empty.

Solution 9 - Java

Instead of using third party lib, use Java 11 isBlank()

    String str1 = "";
    String str2 = "   ";
    Character ch = '\u0020';
    String str3 =ch+" "+ch;
   
    System.out.println(str1.isEmpty()); //true
    System.out.println(str2.isEmpty()); //false
    System.out.println(str3.isEmpty()); //false            

    System.out.println(str1.isBlank()); //true
    System.out.println(str2.isBlank()); //true
    System.out.println(str3.isBlank()); //true

Solution 10 - Java

public static boolean isEmpty(String ptext) {
 return ptext == null || ptext.trim().length() == 0;
}

public static boolean isBlank(String ptext) {
 return ptext == null || ptext.trim().length() == 0;
}

Both have the same code how will isBlank handle white spaces probably you meant isBlankString this has the code for handling whitespaces.

public static boolean isBlankString( String pString ) {
 int strLength;
 if( pString == null || (strLength = pString.length()) == 0)
 return true;
 for(int i=0; i < strLength; i++)
 if(!Character.isWhitespace(pString.charAt(i)))
 return false;
 return false;
}

Solution 11 - Java

The bottom line is :

isEmpty take " " as a character but isBlank not. Rest both are same.

Solution 12 - Java

I am answering this because it's the top result in Google for "String isBlank() Method".

If you are using Java 11 or above, you can use the String class isBlank() method. This method does the same thing as Apache Commons StringUtils class.

I have written a small post on this method examples, read it here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNSAView Question on Stackoverflow
Solution 1 - JavaarshajiiView Answer on Stackoverflow
Solution 2 - JavanileshView Answer on Stackoverflow
Solution 3 - JavayallamView Answer on Stackoverflow
Solution 4 - JavachutView Answer on Stackoverflow
Solution 5 - JavaOctavia TogamiView Answer on Stackoverflow
Solution 6 - JavaDefaultView Answer on Stackoverflow
Solution 7 - Javadisha4mouryaView Answer on Stackoverflow
Solution 8 - Javauser4555388View Answer on Stackoverflow
Solution 9 - JavaravthiruView Answer on Stackoverflow
Solution 10 - JavaApoorva DhanvanthriView Answer on Stackoverflow
Solution 11 - JavaMuhammad Zahab Ahmad KhanView Answer on Stackoverflow
Solution 12 - JavaPankajView Answer on Stackoverflow