Check if string is empty in Java example shows how to check if string is empty in Java using the equal method, the length method, isEmpty method, or using apache commons.
How to check if string is empty in Java?
There are several ways using which you can check the string.
1) Check if a string is empty using the equals method of the String class
You can check using the equals
method of the String class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
String[] strValues = { "", null, " ", "Hello", " Hello " }; for(String str : strValues){ if(str.equals("")){ System.out.println("String: " + str + ", is empty"); }else{ System.out.println("String: " + str + ", is not empty"); } } |
Output
1 2 3 |
String: , is empty Exception in thread "main" java.lang.NullPointerException at com.javacodeexamples.stringexamples.StringEmptyExample.main(StringEmptyExample.java:23) |
The equals
method throws NullPointerException if the string is null. It is always recommended to check for the null string before calling any methods.
To avoid NullPointerException in your code, use the below given code.
1 2 3 4 5 6 7 8 |
for(String str : strValues){ if(str != null && str.equals("")){ System.out.println("String: " + str + ", is empty"); }else{ System.out.println("String: " + str + ", is not empty"); } } |
Output
1 2 3 4 5 |
String: , is empty String: null, is not empty String: , is not empty String: Hello, is not empty String: Hello , is not empty |
You can also use below given code instead of checking both the conditions (not null and empty).
1 2 3 4 5 6 7 8 |
for(String str : strValues){ if("".equals(str) ){ System.out.println("String: " + str + ", is empty"); }else{ System.out.println("String: " + str + ", is not empty"); } } |
Output
1 2 3 4 5 |
String: , is empty String: null, is not empty String: , is not empty String: Hello, is not empty String: Hello , is not empty |
2) Using the length method of the String class
You can also use length
method of String class to check. The length
method returns 0 if the string is empty.
1 2 3 4 5 6 7 8 |
for(String str : strValues){ if(str != null && str.length() == 0 ){ System.out.println("String: " + str + ", is empty"); }else{ System.out.println("String: " + str + ", is not empty"); } } |
Output
1 2 3 4 5 |
String: , is empty String: null, is not empty String: , is not empty String: Hello, is not empty String: Hello , is not empty |
3) Using the isEmpty method of the String class (Java 1.6 and later)
If you are using Java 1.6 or later version, you can use isEmpty
method of the String class as given below.
1 2 3 4 5 6 7 8 |
for(String str : strValues){ if(str != null && str.isEmpty() ){ System.out.println("String: " + str + ", is empty"); }else{ System.out.println("String: " + str + ", is not empty"); } } |
Output
1 2 3 4 5 |
String: , is empty String: null, is not empty String: , is not empty String: Hello, is not empty String: Hello , is not empty |
4) Using the Apache Commons library
The Apache commons library provides many useful utilities. Use the isEmpty
method of the StringUtils class to check as given below.
1 2 3 4 5 6 7 8 |
for(String str : strValues){ if(StringUtils.isEmpty(str) ){ System.out.println("String: " + str + ", is empty"); }else{ System.out.println("String: " + str + ", is not empty"); } } |
Output
1 2 3 4 5 |
String: , is empty String: null, is empty String: , is not empty String: Hello, is not empty String: Hello , is not empty |
As you might have observed from the output, the isEmpty
method also handles the null very well and returns true.
Alternatively, you can also use the isBlank
method of the StringUtils class which also checks if the string contains only white spaces as given below.
1 2 3 4 5 6 7 8 |
for(String str : strValues){ if(StringUtils.isBlank(str) ){ System.out.println("String: " + str + ", is empty"); }else{ System.out.println("String: " + str + ", is not empty"); } } |
Output
1 2 3 4 5 |
String: , is empty String: null, is empty String: , is empty String: Hello, is not empty String: Hello , is not empty |
Check out the how to check if the StringBuilder or StringBuffer is empty and how to check if the ArrayList is empty examples as well.
This example is a part of the String in Java tutorial.
Please let me know your views in the comments section below.