344

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)?

rmoestl
  • 3,059
  • 5
  • 24
  • 38
NSA
  • 5,689
  • 8
  • 37
  • 48
  • 9
    Might be worth mentioning that there's also a [`StringUtils.isEmpty(foo)`](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isEmpty(java.lang.String)) which helps you avoid null pointers, just like `isBlank`, but doesn't check for whitespace characters. – Xavi Jun 24 '14 at 19:32

14 Answers14

643

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()

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 1
    You couldn't call `.isBlank()` or `.isEmpy()` on a `null` anyways, since they are methods on a `String`. – Mr. Polywhirl Feb 03 '23 at 18:53
  • @Mr.Polywhirl actually, you can: eg on an uninitialized String object (or one that's explicitly set to null). – Yury May 25 '23 at 19:50
167

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
nilesh
  • 14,131
  • 7
  • 65
  • 79
54

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:

ChrisOdney
  • 6,066
  • 10
  • 38
  • 48
yallam
  • 1,224
  • 1
  • 12
  • 12
20

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

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

will throw a NullPointerException if foo is null.

chut
  • 695
  • 4
  • 7
  • 4
    There is a bigger difference than that; see my answer. – arshajii May 02 '14 at 00:56
  • 3
    This is incorrect. String.isEmpty() will return true if it is null. At least if you are talking about the apache.commons.lang version. I'm not sure about the Spring version. – ryoung May 26 '15 at 13:52
  • 1
    The meaning of my comment was edited away (to be fair it could have been more clear from the get-go); I was not comparing StringUtils.isBlank() to StringUtils.isEmpty() - rather StringUtils.isBlank() to the OP's value.isEmpty() – chut May 27 '15 at 17:15
  • 2
    chut's answer is correct. If java String foo is null, then foo.isEmpty() throws NullPointerException. The apache StringUtils.isBlank(foo) returns true even if foo is null. – user2590805 Oct 05 '18 at 08:53
9

StringUtils.isBlank also returns true for just whitespace:

isBlank(String str)

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

Community
  • 1
  • 1
Octavia Togami
  • 4,186
  • 4
  • 31
  • 49
8

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
disha4mourya
  • 369
  • 4
  • 7
7

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

Default
  • 16,020
  • 3
  • 24
  • 38
5

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.

user4555388
  • 51
  • 1
  • 1
4

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
ravthiru
  • 8,878
  • 2
  • 43
  • 52
2
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;
}
2

The bottom line is :

isEmpty take " " as a character but isBlank not. Rest both are same.
Muhammad Zahab
  • 1,049
  • 10
  • 21
2

Instead of using a third-party library, use Java 11 isBlank()

System.out.println("".isEmpty());                      //true
System.out.println(" ".isEmpty());                     //false
System.out.println(('\u0020'+" "+'\u0020').isEmpty()); //false

System.out.println("".isBlank());                      //true
System.out.println(" ".isBlank());                     //true
System.out.println(('\u0020'+" "+'\u0020').isBlank()); //true

If you want to use Java 8 and need isBlank function then try to use third-party library org.apache.commons.lang3.StringUtils

StringUtils.isBlank()

System.out.println(StringUtils.isBlank(null));      //true
System.out.println(StringUtils.isBlank(""));        //true
System.out.println(StringUtils.isBlank(" "));       //true
System.out.println(StringUtils.isBlank("bob"));     //false
System.out.println(StringUtils.isBlank("  bob  ")); //false

StringUtils.isEmpty

System.out.println(StringUtils.isEmpty(null));      // = true
System.out.println(StringUtils.isEmpty(""));        //= true
System.out.println(StringUtils.isEmpty(" "));       // = false
System.out.println(StringUtils.isEmpty("foo"));     // = false
System.out.println(StringUtils.isEmpty("  foo  ")); //= false
Fazal Haroon
  • 845
  • 2
  • 10
  • 20
-1

StringUtils.isBlank(myStr) checks if the String myStr is whitespace, empty("") or null.

iem
  • 93
  • 1
  • 1
  • 11
-4

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.

Pankaj
  • 5,132
  • 3
  • 28
  • 37
  • Downvoted because it's not true that Java 11's `isBlank` would be equivalent to `StringUtils.isBlank()` due to missing `null` check in the Java 11 version. – khituras Sep 14 '21 at 09:03