Checking for Empty or Blank Strings in Java

1. Introduction

In this tutorial, we’ll go through some ways of checking for empty or blank strings in Java. We’ve got some native language approaches as well as a couple of libraries.

2. Empty vs. Blank

It’s, of course, 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 only, then we call it blank.

For Java, whitespaces are characters like spaces, tabs and so on. Have a look at Character.isWhitespace for examples.

3. Empty Strings

3.1. With Java 6 and Above

If we are 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 will 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.

And also remember that Strings are immutable, so calling trim won’t actually change the underlying string.

5. Bean Validation

Another way to check for blank strings is regular expressions. This comes handy for instance with Java Bean Validation:

@Pattern(regexp = "\\A(?!\\s*\\Z).+")
String someString;

The given regular expression ensures that empty or blank strings will not 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:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>

Among other things, this gives us StringUtils.

This class comes with methods like isEmptyisBlank 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:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>

Guavas Strings class comes with a method Strings.isNullOrEmpty:

Strings.isNullOrEmpty(string)

It checks whether a given string is null or empty, but it will not check for whitespace-only strings.

8. 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 samples over on GitHub.