Documente Academic
Documente Profesional
Documente Cultură
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
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
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
Javarevisited
Like 4,660 people like Javarevisited.
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
} 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 ; }
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
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
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
Checks
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
Projects
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...
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 . . .
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