Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringUtils.isBlank() vs String.isEmpty()

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

like image 414
NSA Avatar asked Oct 19 '22 10:10

NSA


People also ask

What is the difference between isBlank and isEmpty?

isBlank() vs isEmpty() The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0. isBlank() method only checks for non-whitespace characters. It does not check the string length.

Which is better isEmpty or isBlank?

Practically speaking, you should generally use String. isBlank if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String. isEmpty when checking fields from the database, which will never contain a string of just whitespace.

What does StringUtils isBlank do?

We can check whether a particular String is empty or not, using isBlank() method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not.

What is the replacement of StringUtils isEmpty?

isBlank() can be used and we can simply say that it is an alternative to the StringUtils. isEmpty() method of Java. If your string only contains whitespaces and you want to treat such types of Strings as an invalid string or empty string, then you can proceed with the StringUtils. isBlank() method.


2 Answers

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

like image 595
arshajii Avatar answered Oct 22 '22 00:10

arshajii


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
like image 163
nilesh Avatar answered Oct 21 '22 22:10

nilesh