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()
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()
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
I also had this problem and found the trick just after saw the source code of String.valueof(). The source code of String.valueof() is below.
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Therefore when you pass a null object, you will get a 'null' String. Therefore your StringUtils.isBlank() will treat is as a String rather than a null object and you will get a 'false' booean value.
Hope it helps.!
I tested those code and they give the same result.
Boolean isBlank = StringUtils.isBlank("");
System.out.println(isBlank);// give true
and
Boolean isBlank = StringUtils.isBlank(null);
System.out.println(isBlank);// give true
The library you want to use is the org.apache.commons.lang.StringUtils and you have to add it to your classpath manually or add it as a dependency to your gradle configuration or any other build tool you are using.
IntelliJ does not know the library out of the box as it is not part of the JDK.
You can use new String class function added since JDK11, the String.isBlank()
Docs here
isBlank
public boolean isBlank()
Returns true if the string is empty or contains only white space codepoints, otherwise false.
Since:
11
Also, as per your use-case advise to go through : StringUtils.isBlank()VsString.isEmpty()