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
)?
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
.
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
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()
will also check for null, whereas this:
String foo = getvalue("foo");
if (foo.isEmpty())
will throw a NullPointerException
if foo
is null.
StringUtils.isBlank
also returns true
for just whitespace:
isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
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() 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.
来源:https://stackoverflow.com/questions/23419087/stringutils-isblank-vs-string-isempty