1. Introduction
In this tutorial, we’ll discuss some ways of checking for empty or blank strings in Java. There are some native language approaches, as well as a couple of libraries.
2. Empty vs. Blank
Of course, it’s pretty common to know when a string is empty or blank, but let’s make sure we’re on the same page with our definitions.
We consider a string to be empty if it’s either null or a string without any length. If a string only consists of whitespace, then we call it blank.
For Java, whitespaces are characters, like spaces, tabs, and so on. We can check out Character.isWhitespace for examples.
3. Empty Strings
3.1. With Java 6 and Above
If we’re at least on Java 6, then the simplest way to check for an empty string is String#isEmpty:
boolean isEmptyString(String string) {
return string.isEmpty();
}
To make it also null-safe, we need to add an extra check:
boolean isEmptyString(String string) {
return string == null || string.isEmpty();
}
3.2. With Java 5 and Below
String#isEmpty was introduced with Java 6. For Java 5 and below, we can use String#length instead:
boolean isEmptyString(String string) {
return string == null || string.length() == 0;
}
In fact, String#isEmpty is just a shortcut to String#length.
4. Blank Strings
Both String#isEmpty and String#length can be used to check for empty strings.
If we also want to detect blank strings, we can achieve this with the help of String#trim. It’ll remove all leading and trailing whitespaces before performing the check:
boolean isBlankString(String string) {
return string == null || string.trim().isEmpty();
}
To be precise, String#trim will remove all leading and trailing characters with a Unicode code less than or equal to U+0020.
Also, we should remember that Strings are immutable, so calling trim won’t actually change the underlying string.
In addition to the above approach, as of Java 11, we can also use the isBlank() method instead of trimming:
boolean isBlankString(String string) {
return string == null || string.isBlank();
}
The isBlank() method is a bit more efficient as well, as it doesn’t create a new String on the heap. As a result, if we’re on Java 11 or above, this is the preferred approach.
5. Bean Validation
Another way to check for blank strings is regular expressions. For instance, this comes in handy with Java Bean Validation:
@Pattern(regexp = "\\A(?!\\s*\\Z).+")
String someString;
The given regular expression ensures that empty or blank strings won’t validate.
6. With Apache Commons
If it’s ok to add dependencies, we can use Apache Commons Lang. This has a host of helpers for Java.
If we use Maven, we need to add the commons-lang3 dependency to our pom.xml:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
Among other things, this gives us StringUtils.
This class comes with methods like isEmpty, isBlank, and so on:
StringUtils.isBlank(string)
This call does the same as our own isBlankString method. It’s null-safe and also checks for whitespaces.
7. With Guava
Another well-known library that brings certain string related utilities is Google’s Guava. Starting with version 23.1, there are two flavors of Guava: android and jre. The Android flavor targets Android and Java 7, whereas the JRE flavor goes for Java 8.
If we’re not targeting Android, we can just add the JRE flavor to our pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
Guavas Strings class comes with the method Strings.isNullOrEmpty:
Strings.isNullOrEmpty(string)
It checks whether a given string is null or empty, but it won’t check for whitespace-only strings.
8. With Spring ObjectUtils
Furthermore, the Spring Core library provided a class named StringUtils which has a method to check if a string is empty. However, the class was deprecated in the Spring 5.3.0 version and above. It’s replaced with the ObjectUtils class.
To use the ObjectUtils class, let’s add the Spring Core dependency to our pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.0</version>
</dependency>
The ObjectUtils class provides the isEmpty() method to check if a string is empty:
ObjectUtils.isEmpty(emptyString)
Like Guava, it won’t check if a string only contains whitespace but checks whether a given string is null or empty.
9. Conclusion
There are several ways to check whether a string is empty or not. Often, we also want to check if a string is blank, meaning that it consists of only whitespace characters.
The most convenient way is to use Apache Commons Lang, which provides helpers such as StringUtils.isBlank. If we want to stick to plain Java, we can use a combination of String#trim with either String#isEmpty or String#length. For Bean Validation, regular expressions can be used instead.
Make sure to check out all these examples over on GitHub.