Friday, February 1, 2013

String in Java is considered empty if its not null and it’s length is
zero. By the way before checking length you should verify that String is not
null because calling length() method on null String
will result in java.lang.NullPointerException.
Empty String is represented by String literal “”.
Definition of empty String may be extended to those String as well which only
contains white space but its an specific requirement and in general String with white space are not considered as empty String in Java. Since String is one of the most
frequently used class and commonly used in method arguments, we often needs to
check if String is empty or not. Thankfully there are multiple ways to find if String is empty in Java or not. You
can also count number of characters in String, as String is represented as character array
and decide if String is empty or not. If count of characters is zero than its
an empty String. In this Java String tutorial we going to see 5 ways to find if
any String in Java is empty or not. Here are our five ways to check empty String
:

3) Checking if String is empty by using isEmpty() method
String, only available from Java 6 onwards.

4) Find if String is empty using Apache commons StringUtils class

5) Using Spring framework’s StringUtils.hasLength() method.

Find
if String is empty by checking length

It's the most easy and popular method to verify if String is empty or
not. You can find length of String by calling

length() method which actually returns number
of characters in String. Be careful to check if String is null before calling length()to avoid NullPointerException. here is
an example to check is String empty using
length:

if(string != null && string.length() == 0){returntrue;}

String
empty using equals method

You can also compare String to empty String
literal "" to check if it’s empty or not. equals method in Java returns
false if other argument is null, so it automatically checks for null string as
well. Here is code example of checking emptiness of String using equals:

You can also check if String is empty or not by using isEmpty() method of String class added in Java6. This
is by far most readable way but you need
to check if String is null or not before calling isEmpty() method.
see code example section for use of isEmpty() method.

String
Empty check using Apache Commons lang StringUtils

Apache commons lang has a StringUtils class
which has static utility methodisEmpty(String
input), which returns true if input string is null or has
length greater than zero. Note this is different than our first method because
it consider null as empty String while we are here only considering zero length
String as empty String. If you are
already using Apache commons lang in your project e.g. for overriding toString method, than
you can use StringUtils instead of writing your own
method. Check example section to see how to use StringUtils.isEmpty(), by the
way here is output of StringUtils for some common input :

StringUtils.isEmpty("")= true

StringUtils.isEmpty(null)= true

StringUtils.isEmpty(" ")= false

StringUtils.isEmpty("fix")= false

StringUtils.isEmpty("fix") = false

Check
if String is Empty in Java - Using Spring

Springis popular Java
framework and most of new projects uses Spring to take benefit of dependency Injection, it provide
StringUtils class for performing common String operation. StringUtils provides
method called hasLength(input String), which returns
true if String is not null and contains any character, including white space. You can also use hasLength to
determine if String is empty in Java or not. Next section has code examples of
all five methods of checking empty string mentioned in this Java tutorial, by
the way here is how hasLength() treats null and empty String :

StringUtils.hasLength("") = false

StringUtils.hasLength(null) = false

StringUtils.hasLength(" ") = true

StringUtils.hasLength("Hello") = true

Code Example to verify if String is empty in
Java

Here is complete code example of How to check if String is empty in Java.
This program combines all approaches we have discussed so fart to check if Java
String is empty or not. One interesting thing to note in this program is How I have used StringUtils from Spring Framework. Since there are two classes with same name but from different package, i.e. StringUtils from Apache and Spring. You can only import one and you need to use other with its fully qualified name to avoid ambiguity.

That’s all on How to check if String is empty in Java. I thing Java
6 isEmpty() method is more readable than any other option but it’s not null safe
which means either write your own method or use hasLength() from
Spring Framework. By the way be careful
with null String as some programmer consider null string as empty String and
even Apache commons StringUtils.isEmpty() method return true for null
String.

@Anonymous, isBlank() and isNotBlank() is also a good choice but they return true even if String contains whitespace, i.e. they are not empty or you can say when length of String is not zero. If your definition of empty String is equal to blank i.e. include white space than isBlank() is a real good choice.

I recommend Guava and its class com.google.common.base.Strings. It contains methods isNullOrEmpty(String string). Full docs here:http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Strings.html#isNullOrEmpty(java.lang.String)