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.

<code>StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false</code>

StringUtils.isNotEmpty()

Negates

isEmpty()

; returns

true

when the input is not empty.

<code>public static boolean isNotEmpty(final CharSequence cs) {
    return !isEmpty(cs);
}</code>

StringUtils.isAnyEmpty()

Returns

true

if **any** of the supplied

CharSequence

arguments is empty or

null

.

<code>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</code>

StringUtils.isNoneEmpty()

Negates

isAnyEmpty()

; returns

true

only when **all** arguments are non‑empty and non‑null.

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

isBlank series

StringUtils.isBlank()

Returns

true

if the input is

null

, empty, or consists solely of whitespace characters.

<code>StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false</code>

StringUtils.isNotBlank()

Negates

isBlank()

; true when the string contains non‑whitespace characters.

<code>public static boolean isNotBlank(final CharSequence cs) {
    return !isBlank(cs);
}</code>

StringUtils.isAnyBlank()

Returns

true

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

<code>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</code>

StringUtils.isNoneBlank()

Negates

isAnyBlank()

; true only when **all** arguments are non‑blank.

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

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
JavaApache 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

login 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.