You are on page 1of 4

7/15/13

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

Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL

Java 6
FR IDA Y , FEB R UA R Y 1, 2013

Program Java

To Java

Java Tutorial
Searc h

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


Recent Posts
String in Java is considered empty if its not null and its length is zero. By the way before checking length you should verify that String is not null because calling l e n g t h ( )method on n u l lString 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 : 1) Checking if String is empty by using S t r i n g . l e n g t h ( ) 2) Find if String is empty by using equals() method of String 3) Checking if String is empty by using i s E m p t y ( )method String, only available from Java 6 onwards. 4) Find if String is empty using Apache commons S t r i n g U t i l sclass 5) Using Spring frameworks S t r i n g U t i l s . h a s L e n g t h ( )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 l e n g t h ( )method which actually returns number of characters in String. Be careful to check if String is null before calling l e n g t h ( ) to avoid NullPointerException. here is an example to check is String empty using length: i f ( s t r i n g! =n u l l& &s t r i n g . l e n g t h ( )= =0 ) {r e t u r nt r u e ;}

When to make a method static in Java Top 5 JQuery books for Beginners and Web developers - Best of lot How SSL, HTTPS and Certificates works in Java web applications Role based Access control using Spring Security and MVC, Mapping LDAP Groups to Authorities for Authorization 10 Java Exception and Error Interview Questions Answers 5 must read books to learn Object Oriented and Java Design patterns - Best of lot

String empty using equals method You can also compare String to empty String literal " "to check if its empty or not. equals method in Java returns false if other argument is null, so it automatically checks for n u l lstring as well. Here is code example of checking emptiness of String using equals: p u b l i cs t a t i cb o o l e a ni s S t r i n g E m p t y B y E q u a l s ( S t r i n gi n p u t ) { r e t u r n" " . e q u a l s ( i n p u t ) ; }

Use isEmpty() method of Java 6 You can also check if String is empty or not by using i s E m p t y ( )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 i s E m p t y ( )method. see code example section for use of i s E m p t y ( )method. String Empty check using Apache Commons lang StringUtils Apache commons lang has a S t r i n g U t i l sclass which has static utility method is E m p t y ( S t r i n gi n p u t ) ,which returns true if input string is n u l lor 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 S t r i n g U t i l sinstead of writing your own method. Check example section to see how to use S t r i n g U t i l s . i s E m p t y ( ) , by the way here is output of S t r i n g U t i l sfor some common input : S t r i n g U t i l s . i s E m p t y ( " " ) S t r i n g U t i l s . i s E m p t y ( n u l l ) S t r i n g U t i l s . i s E m p t y ( "" ) S t r i n g U t i l s . i s E m p t y ( " f i x " ) =t r u e =t r u e =f a l s e =f a l s e

Follow Us
Follow @javinpaul 3,239 follow ers

Javarevisited on

Follow

+3,257

S t r i n g U t i l s . i s E m p t y ( " f i x " )=f a l s e

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 S t r i n g U t i l sclass for performing common String operation. S t r i n g U t i l sprovides method called h a s L e n g t h ( i n p u t S t r i n g ) , which returns true if String is not null and contains any character, including white space. You can also use h a s L e n g t hto determine if String is empty in Java or not. Next section has code examples of all five methods of checking

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

1/4

7/15/13

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


empty string mentioned in this Java tutorial, by the way here is how h a s L e n g t h ( )treats null and empty String : S t r i n g U t i l s . h a s L e n g t h ( " " )=f a l s e S t r i n g U t i l s . h a s L e n g t h ( n u l l )=f a l s e S t r i n g U t i l s . h a s L e n g t h ( "" )=t r u e S t r i n g U t i l s . h a s L e n g t h ( " H e l l o " )=t r u e

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. i m p o r to r g . a p a c h e . c o m m o n s . l a n g . S t r i n g U t i l s ; p u b l i cc l a s sS t r i n g E m p t y T e s t{ p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ){ S t r i n gi n p u t 1=" " ; S t r i n gi n p u t 2=n u l l ; S t r i n gi n p u t 3= " a b c " ; / / d e t e r m i n ei fS t r i n gi se m p t yu s i n gl e n g t hm e t h o d,a l s oc h e c k si fs t r i n gi sn u l l S y s t e m . o u t . p r i n t l n ( " c h e c k i n gi fS t r i n ge m p t yu s i n gl e n g t h " ) ; S y s t e m . o u t . p r i n t l n ( " S t r i n g"+i n p u t 1+"i se m p t y: "+ i s S t r i n g E m p t y ( i n p u t 1 )) ; S y s t e m . o u t . p r i n t l n ( " S t r i n g"+i n p u t 2+"i se m p t y: "+ i s S t r i n g E m p t y ( i n p u t 2 )) ; S y s t e m . o u t . p r i n t l n ( " S t r i n g"+i n p u t 3+"i se m p t y: "+ i s S t r i n g E m p t y ( i n p u t 3 )) ; / / d e t e r m i n ei fS t r i n gi se m p t yu s i n ge q u a l sm e t h o d S y s t e m . o u t . p r i n t l n ( " f i n di fS t r i n ge m p t yu s i n ge q u a l s " ) ; S y s t e m . o u t . p r i n t l n ( " S t r i n g"+i n p u t 2+"i se m p t y: "+ i s S t r i n g E m p t y B y E q u a l s ( i n p u t 2 ) ) ; / / d e t e r m i n ei fS t r i n gi se m p t yu s i n gi s E m p t yo fJ a v a6 S y s t e m . o u t . p r i n t l n ( " f i n di fS t r i n ge m p t yu s i n gi s E m p t ym e t h o do fJ a v a6 " ) ; S y s t e m . o u t . p r i n t l n ( " S t r i n g"+i n p u t 3+"i se m p t y: "+i n p u t 3 . i s E m p t y ( ) ) ; / / d e t e r m i n ei fS t r i n gi se m p t yb yA p a c h ec o m m o n sS t r i n g U t i l s S y s t e m . o u t . p r i n t l n ( " c h e c ki fS t r i n ge m p t yb yc o m m o n sS t r i n g U t i l s " ) ; S y s t e m . o u t . p r i n t l n ( " S t r i n g"+i n p u t 2+"i se m p t y: "+ S t r i n g U t i l s . i s E m p t y ( i n p u t 2 ) ) ; / / d e t e r m i n ei fS t r i n gi se m p t yb yS p r i n gf r a m e w o r kS t r i n g U t i l sh a s L e n g t hm e t h o d S y s t e m . o u t . p r i n t l n ( " c h e c ki fS t r i n ge m p t yb yS p r i n gf r a m e w o r kS t r i n g U t i l s " ) ; S y s t e m . o u t . p r i n t l n ( " S t r i n g"+i n p u t 2+"i se m p t y: "+ o r g . s p r i n g f r a m e w o r k . u t i l . S t r i n g U t i l s . h a s L e n g t h ( i n p u t 2 ) ) ;

Javarevisited
Like 4,660 people like Javarevisited.

F acebook social plugin

Recommendd Books Java Programming and Design Pattern Spring Framework Subscribe by email: Subscribe By Javin Paul Subscribe To This Blog Free Posts Comments Followers Join this site
w ith Google Friend Connect

Members (1250) More

} p u b l i cs t a t i cb o o l e a ni s S t r i n g E m p t y ( S t r i n gi n p u t ) { i f ( i n p u t! =n u l l& &i n p u t . l e n g t h ( )= =0 ) { r e t u r nt r u e ; } r e t u r nf a l s e ; }

Already a member? Sign in

Java Sample Programs Java Classes Java Interview Questions


Blog Archive

p u b l i cs t a t i cb o o l e a ni s S t r i n g E m p t y B y E q u a l s ( S t r i n gi n p u t ) { r e t u r n" " . e q u a l s ( i n p u t ) ; } } O u t p u t : c h e c k i n gi fS t r i n ge m p t yu s i n gl e n g t h S t r i n g i se m p t y: t r u e S t r i n gn u l li se m p t y: f a l s e S t r i n ga b ci se m p t y: f a l s e f i n di fS t r i n ge m p t yu s i n ge q u a l s S t r i n gn u l li se m p t y: f a l s e

2013 (97) July (4) June (9) May (14) April (18) March (16) February (18) How to use ConcurrentHashMap in Java Example Tut... How to get Key from Value in Hashtable, HashMap or... 2 ways to combine Arrays in Java Integer,

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

2/4

7/15/13
S t r i n ga b ci se m p t y: f a l s e c h e c ki fS t r i n ge m p t yb yc o m m o n sS t r i n g U t i l s S t r i n gn u l li se m p t y: t r u e

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


f i n di fS t r i n ge m p t yu s i n gi s E m p t ym e t h o do fJ a v a6

String... How to Swap Two Numbers without Temp or Third vari... How to install JDK 7 on Windows 8 - Java Programmi... How to create and call stored procedure in MySQL w... How to set Java Path and Classpath in Windows 8 an... How to convert XMLGregorianCalendar to Date to XML... How to add leading zeros to Integers in Java Str... 5 JSTL Core IF Tag Examples in JSP - Tutorial How to convert JSON String to Java object Jackso... How to Fix java.net.ConnectException: Connection r... Top 5 Concurrent Collections from JDK 5 and 6 Java... How to Join Multiple Threads in Java - Thread Join... How to disable submit button in HTML JavaScript to... What is Timer and TimerTask in Java Tutorial Exa...

c h e c ki fS t r i n ge m p t yb yS p r i n gf r a m e w o r kS t r i n g U t i l s S t r i n gn u l li se m p t y: f a l s e

Thats all on How to check if String is empty in Java. I thing Java 6 i s E m p t y ( ) method is more readable than any other option but its not null safe which means either write your own method or use h a s L e n g t h ( )from Spring Framework. By the way be careful with null String as some programmer consider null string as empty String and even Apache commons S t r i n g U t i l s . i s E m p t y ( )method return true for null String. Related Java String Tutorials and Questions from Javarevisited Blog Why character array is better than String for storing password in Java How to replace characters on String in Java How to split String in Java with example How to convert String to date in Java How to convert String to Integer in Java 10 advanced String Interview Questions in Java

Please share with your friends if like this article

How to Fix Must Override a Superclass Method Error... 5 ways to check if String is empty in Java examp... January (18)

Examples
You might like:

Security Systems

Code Example

You can only

Checks

2012 (218) 2011 (145) 2010 (33)

How to get Key from Value in Hashtable, HashMap or Map in Java How to Fix Must Override a Superclass Method Error Eclipse IDE Java What is Inheritance in Java and OOPS Tutorial - Example 10 Object Oriented Design Principles Java Programmer should know
Recommended by

References Posted by Javin Paul at 4:51 AM Labels: coding, core java, programming, spring
+7 Recommend this on Google

Java API documentation JDK 6 Spring framework doc Struts

Job interview questions Environmentally Friendly

Spring Framework Examples

Projects

Interview Questions Code Example

ANT Maven JDK 7 API MySQL Linux Eclipse Copyright by Javin Paul 2012. Powered by Blogger.

Security Systems

6 comments:
Jiri Pinkas said... I don't think using StringUtils from Spring framework should be considered best practice, because in their documentation is: Mainly for internal use within the framework; consider Jakarta's Commons Lang for a more comprehensive suite of String utilities. see: http://static.springsource.org/spring/docs/current/javadocapi/org/springframework/util/StringUtils.html February 1, 2013 at 5:24 AM Anonymous said... I also prefer using apache's StringUtils isBlank() and isNotBlank(). February 4, 2013 at 4:10 AM Javin @ ClassLoader in Java said... @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. StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true

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

3/4

7/15/13
February 4, 2013 at 4:58 PM Anonymous said...

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

I recommend Guava and its class com.google.common.base.Strings. It contains methods isNullOrEmpty(String string). Full docs here: http://docs.guavalibraries.googlecode.com/git/javadoc/com/google/common/base/Strings.html#isNullOrEmpty(java.lang.String ) February 22, 2013 at 3:51 AM Anonymous said... Another option to StringUtils.isBlank(" "); could be " ".trim().length() == 0 February 22, 2013 at 8:12 AM Anonymous said... What is difference between empty String and blank String? March 4, 2013 at 1:14 AM

Post a Comment
E n t e ry o u rc o m m e n t . . .

Comment as: Google Account Publish Preview

Newer Post Subscribe to: Post Comments (Atom)

Home

Older Post

About Me

Privacy Policy

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

4/4

You might also like