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
)?
This question is related to
java
string
string-utils
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:
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.
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;
}
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.
StringUtils.isBlank(foo)
will perform a null check for you. If you perform foo.isEmpty()
and foo
is null, you will raise a NullPointerException.
StringUtils.isBlank
also returns true
for just whitespace:
isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank()
will also check for null, whereas this:
String foo = getvalue("foo");
if (foo.isEmpty())
will throw a NullPointerException
if foo
is null.
The bottom line is :
isEmpty take " " as a character but isBlank not. Rest both are same.
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
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
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
Source: Stackoverflow.com