Selasa, 24 Juni 2014

5 ways to check if String is empty in Java - examples




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
a
nd 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
:







1) Checking if String is empty by using String.length()


2) Find  if String is empty by
using equals() method of 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){ return true; }








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:






public static
boolean isStringEmptyByEquals(String input){

        return "".equals(input);

}









Use
isEmpty() method of Java 6


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 method
isEmpty(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, t
han
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


Spring is 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



How to check if String is empty in Java with ExampleHere 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.






import org.apache.commons.lang.StringUtils;



public class
StringEmptyTest {



    public static void
main(String args[]) {

 

        String input1 = "";

        String input2 = null;

        String input3 ="abc";

     

        //determine if
String is empty using length method , also checks if string is null


        System.out.println("checking if String empty using length");

        System.out.println("String " + input1 + " is empty :" +isStringEmpty(input1) );

        System.out.println("String " + input2 + " is empty :" +isStringEmpty(input2) );

        System.out.println("String " + input3 + " is empty :" +isStringEmpty(input3) );

     

        //determine if
String is empty using equals method


        System.out.println("find if String empty using equals");

        System.out.println("String " + input2 + " is empty :" +isStringEmptyByEquals(input2) );

     

         //determine if
String is empty using isEmpty of Java 6


        System.out.println("find if String empty using isEmpty method of Java
6"
);

        System.out.println("String " + input3 + " is empty :" + input3.isEmpty());

     

        //determine if
String is empty by Apache commons StringUtils


        System.out.println("check if String empty by commons StringUtils");

        System.out.println("String " + input2 + " is empty :" + StringUtils.isEmpty(input2));

     

        //determine if
String is empty by Spring framework StringUtils hasLength method


        System.out.println("check if String empty by Spring framework StringUtils");

        System.out.println("String " + input2 + " is empty :" + org.springframework.util.StringUtils.hasLength(input2));



     

    }

 

    public static boolean
isStringEmpty(String input){

        if(input != null
&& input.length() == 0){

            return
true;

        }

        return false;

    }

 

    public static boolean
isStringEmptyByEquals(String input){

        return "".equals(input);

    }

}



Output:

checking if String empty using length

String  is empty :true

String null is empty :false

String abc is empty :false




find if String empty using equals

String null is empty :false




find if String empty using isEmpty
method of Java 6

String abc is empty :false




check if String empty by commons
StringUtils

String null is empty :true




check if String empty by Spring
framework StringUtils

String null is empty :false









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.





Related Java String Tutorials and Questions from Javarevisited
Blog






























Source:http://javarevisited.blogspot.com/2013/02/5-ways-to-check-if-string-is-empty-in-java-examples.html

1 komentar:

  1. Best explanation. Thanks for sharing.
    https://www.flowerbrackets.com/difference-between-equal-operator-vs-equals-method-java/

    BalasHapus