Fundamentals 7 min read

Mastering Apache Commons StringUtils: When to Use isEmpty, isBlank, and Their Variants

This article explains the differences between Apache Commons StringUtils methods such as isEmpty, isBlank, and their extended variants, provides practical code examples for each, and guides developers on choosing the appropriate utility for handling null, empty, or whitespace strings in Java.

macrozheng
macrozheng
macrozheng
Mastering Apache Commons StringUtils: When to Use isEmpty, isBlank, and Their Variants

New Java developers often confuse methods like isEmpty and isBlank in org.apache.commons.lang3.StringUtils. This guide clarifies their behavior and introduces additional variants such as isAnyEmpty, isNoneEmpty, isAnyBlank, and isNoneBlank.

isEmpty series

StringUtils.isEmpty()

Returns true if the given CharSequence is null or has a length of zero. Note that a single space character is not considered empty.

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false

StringUtils.isNotEmpty()

Negates isEmpty(); returns true when the input is not empty.

public static boolean isNotEmpty(final CharSequence cs) {
    return !isEmpty(cs);
}

StringUtils.isAnyEmpty()

Returns true if **any** of the supplied CharSequence arguments is empty or null.

StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, "foo") = true
StringUtils.isAnyEmpty("", "bar") = true
StringUtils.isAnyEmpty("bob", "") = true
StringUtils.isAnyEmpty("bob", null) = true
StringUtils.isAnyEmpty(" ", "bar") = false
StringUtils.isAnyEmpty("foo", "bar") = false

StringUtils.isNoneEmpty()

Negates isAnyEmpty(); returns true only when **all** arguments are non‑empty and non‑null.

public static boolean isNoneEmpty(final CharSequence... css) {
    return !isAnyEmpty(css);
}

isBlank series

StringUtils.isBlank()

Returns true if the input is null, empty, or consists solely of whitespace characters.

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

StringUtils.isNotBlank()

Negates isBlank(); true when the string contains non‑whitespace characters.

public static boolean isNotBlank(final CharSequence cs) {
    return !isBlank(cs);
}

StringUtils.isAnyBlank()

Returns true if **any** of the supplied arguments is blank (null, empty, or whitespace only).

StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, "foo") = true
StringUtils.isAnyBlank(null, null) = true
StringUtils.isAnyBlank("", "bar") = true
StringUtils.isAnyBlank("bob", "") = true
StringUtils.isAnyBlank("bob", null) = true
StringUtils.isAnyBlank(" ", "bar") = true
StringUtils.isAnyBlank("foo", "bar") = false

StringUtils.isNoneBlank()

Negates isAnyBlank(); true only when **all** arguments are non‑blank.

public static boolean isNoneBlank(final CharSequence... css) {
    return !isAnyBlank(css);
}

Other useful StringUtils methods

For a complete list and detailed documentation, refer to the official Apache Commons Lang API:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Apache Commonsstring handlingStringUtilsutility methods
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.