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.
New Java developers often confuse methods like
isEmptyand
isBlankin
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
trueif the given
CharSequenceis
nullor 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
truewhen the input is not empty.
<code>public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}</code>StringUtils.isAnyEmpty()
Returns
trueif **any** of the supplied
CharSequencearguments 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
trueonly 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
trueif 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
trueif **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
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.