Mastering Apache Commons StringUtils: When to Use isEmpty, isBlank, and More
This article explains the differences among Apache Commons Lang's StringUtils methods—such as isEmpty, isBlank, isNotEmpty, isAnyEmpty, isNoneEmpty, and their variants—providing code examples, usage guidelines, and practical tips for Java developers to avoid common pitfalls when handling empty or blank strings.
isEmpty series
StringUtils.isEmpty()
Determines whether a CharSequence is null or has length zero. Note that a string containing only a space is not considered empty.
<code>StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
</code>StringUtils.isNotEmpty()
Returns the opposite of isEmpty.
<code>public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
</code>StringUtils.isAnyEmpty()
Returns true if any of the supplied CharSequences is null or empty.
<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
public static boolean isAnyEmpty(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css) {
if (isEmpty(cs)) {
return true;
}
}
return false;
}
</code>StringUtils.isNoneEmpty()
Returns true only when none of the supplied CharSequences are null or empty.
<code>public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}
</code>isBlank series
StringUtils.isBlank()
Checks whether a CharSequence is null, empty, or consists solely of whitespace.
<code>StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
</code>StringUtils.isNotBlank()
Negates isBlank.
<code>public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}
</code>StringUtils.isAnyBlank()
Returns true if any supplied CharSequence is null, empty, or whitespace only.
<code>public static boolean isAnyBlank(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css) {
if (isBlank(cs)) {
return true;
}
}
return false;
}
</code>StringUtils.isNoneBlank()
Returns true only when none of the supplied CharSequences are blank.
<code>public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}
</code>For a complete list of StringUtils methods, refer to the official Apache Commons Lang documentation.
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.