- 5 ways to check if String is empty in Java — examples
- 1. Find if String is empty by checking the length
- 2. String empty using the equals method
- 3. Use isEmpty() method of Java 6
- 4. String Empty check using Apache Commons lang StringUtils
- 5. Check if String is Empty in Java — Using Spring
- Code Example to verify if String is empty in Java
- Java: Check if String is Null, Empty or Blank
- Using the Length of the String
- Using the isEmpty() Method
- Using the equals() Method
- Using the StringUtils Class
- Free eBook: Git Essentials
- Conclusion
5 ways to check if String is empty in Java — examples
String in Java is considered empty if it’s not null and its 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 “” . The definition of empty String may be extended to those String as well which only contains white space but it’s a specific requirement and in general String with white space is not considered as empty String in Ja va. Since String is one of the most frequently used classes and commonly used in method arguments, we often need 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 the number of characters in String, as String is represented as a character array a nd decide if String is empty or not. If the count of characters is zero then 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 the isEmpty() method String, only available from Java 6 onwards.
5) Using Spring framework’s StringUtils.hasLength() method.
By the way, if you are new to the Spring framework then I also suggest you join a comprehensive and up-to-date course to learn Spring in depth. If you need recommendations, I highly suggest you take a look at these best Spring Framework courses which contain the most comprehensive and hands-on course to learn modern Spring. It’ also the most up-to-date and covers Spring 5. It’s also very affordable and you can buy in just $10 on Udemy sales which happen every now and then.
1. Find if String is empty by checking the length
It’s the easiest and popular method to verify if String is empty or not. You can find the length of String by calling the length() method which actually returns a number of characters in String. Be careful to check if String is null before calling length() to av oid NullPointerException. her e is an example to check is String empty using length:
2. String empty using the equals method
You can also compare String to e mpty String literal «» to check if it’s empty or n ot. equals method in Java return s false if another argument is null, so it automatically checks for the null string as well. Here is a code example of checking emptiness of String using equals:
public static boolean isStringEmptyByEquals ( String input ) <
return «» . equals ( input ) ;
>
3. Use isEmpty() method of Java 6
You can also check if the String is empty or not by using the isEmpty() method of the String class added in Java6. This is by far the most readable way but you need to check if String is null or not before calling the isEmpty() method. see code example section for use of isEmpty() method.
4. String Empty check using Apache Commons lang StringUtils
Apache commons-lang has a StringUtils class which has static utility method i sEmpty(String input), which returns true if the input string is null or has a length greater than zero. Note this is different than our first method because it considers null as empty String while we are here only considering zero-length String as an empty String.
If you are already using Apache commons-lang in your project e.g. for overriding the toString method, then you can use StringUtils instead of writing your own method. Check the example section to see how to use StringUtils.isEmpty() , by the way here is the output of StringUtils for some common input :
5. Check if String is Empty in Java — Using Spring
Spring is a popular Java framework and most of the new projects use Spring to take be nefit of dependency Injection, it provides StringUtils class for performing common String operation. StringUtils provides a method called hasLength(input String) , which returns true if the 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 :
Code Example to verify if String is empty in Java
Here is a 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 The 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 the same name but from different packages, i.e. StringUtils from Apache and Spring. You can only import one and you need to use others 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
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 programmers consider null string as empty String and even Apache commons StringUtils.isEmpty() method return true for null String.
That’s all about how to check if the given String is empty in Java or not. If you like this Java tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P.S. — If you want to learn how to develop RESTful Web Service using Spring MVC in-depth, I suggest you join the REST with Spring certification class by Eugen Paraschiv. One of the best courses to learn REST with Spring MVC.
Java: Check if String is Null, Empty or Blank
In Java, there is a distinct difference between null , empty, and blank Strings.
- An empty string is a String object with an assigned value, but its length is equal to zero.
- A null string has no value at all.
- A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn’t of 0 length.
String nullString = null; String emptyString = ""; String blankString = " ";
In this tutorial, we’ll look at how to check if a String is Null, Empty or Blank in Java.
Using the Length of the String
As mentioned before, a string is empty if its length is equal to zero. We will be using the length() method, which returns the total number of characters in our string.
String blankString = " "; if (blankString == null || blankString.length() == 0) System.out.println("This string is null or empty"); else System.out.println("This string is neither null nor empty");
The code above will produce the following output:
This string is null or empty
The String is blank, so it’s obviously neither null nor empty. Now, based just on the length, we can’t really differentiate between Strings that only contain whitespaces or any other character, since a whitespace is a Character .
Note: It’s important to do the null -check first, since the short-circuit OR operator || will break immediately on the first true condition. If the string, in fact, is null , all other conditions before it will throw a NullPointerException .
Using the isEmpty() Method
The isEmpty() method returns true or false depending on whether or not our string contains any text. It’s easily chainable with a string == null check, and can even differentiate between blank and empty strings:
String string = "Hello there"; if (string == null || string.isEmpty() || string.trim().isEmpty()) System.out.println("String is null, empty or blank."); else System.out.println("String is neither null, empty nor blank");
The trim() method removes all whitespaces to the left and right of a String, and returns the new sequence. If the String is blank, after removing all whitespaces, it’ll be empty, so isEmpty() will return true .
Running this piece of code will give us the following output:
String is neither null, empty nor blank
Using the equals() Method
The equals() method compares the two given strings based on their content and returns true if they’re equal or false if they are not:
String string = "Hello there"; if (string == null || string.equals("") || string.trim().equals("")) System.out.println("String is null, empty or blank"); else System.out.println("String is neither null, empty nor blank");
In much the same fashion as the before, if the trimmed string is «» , it was either empty from the get-go, or was a blank string with 0..n whitespaces:
String is neither null, empty nor blank
Using the StringUtils Class
The Apache Commons is a popular Java library that provides further functionality. StringUtils is one of the classes that Apache Commons offers. This class contains methods used to work with Strings , similar to the java.lang.String .
If you’re unfamiliar with Apache Commons’ helper classes, we strongly suggest reading our Guide to the StringUtils class.
Since we’ll be using Apache Commons for this approach, let’s add it as a dependency:
dependency> groupId>org.apache.commons groupId> artifactId>commons-lang3 artifactId> version>3.11 version> dependency>
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
One of the key differences between StingUtils and String methods is that all methods from the StringUtils class are null-safe. It additionally provides a few methods that we can leverage for this, including StringUtils.isEmpty() and StringUtils.isBlank() :
String nullString = null; if (nullString == null) < System.out.println("String is null"); > else if (StringUtils.isEmpty(nullString)) < System.out.println("String is empty"); > else if (StringUtils.isBlank(nullString)) < System.out.println("String is blank"); >
In addition to these, their inverse methods also exist: StringUtils.isNotEmpty() and StringUtils.isNotBlank() , though, you can achieve the same functionality by using the NOT ( ! ) operator:
if (StringUtils.isNotEmpty("")) System.out.println("String is not empty"); // Equivalent to if (!StringUtils.isEmpty("")) System.out.println("String is not empty");
Conclusion
A string is an object that represents a sequence of characters. Java provides many different methods for string manipulation. In this article, we have used some of these methods such as isEmpty() , equals() , StringUtils.isEmpty() and length() to check if the String is null , empty or blank.