Understanding Apache Commons Lang StringUtils: isEmpty, isBlank, and Related Methods
This article introduces the Apache Commons Lang StringUtils utility class, explaining the behavior and usage of its isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank methods with code examples and detailed descriptions.
The article presents the org.apache.commons.lang3.StringUtils class from Apache Commons Lang, a collection of static utilities for handling String and CharSequence values in Java.
isEmpty series
StringUtils.isEmpty() returns true for null or an empty string, but treats a string containing only a space as non‑empty.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = falseThe implementation (since version 3.0) checks for null or zero length:
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}StringUtils.isNotEmpty() simply negates isEmpty() :
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") = falseImplementation:
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;
}StringUtils.isNoneEmpty(...) returns true only when all supplied arguments are non‑empty:
/**
* Checks if none of the CharSequences are empty ("") or null.
*/
public static boolean isNoneEmpty(final CharSequence... css) {
// implementation omitted for brevity
}isBlank series
StringUtils.isBlank() treats null , empty strings, and strings containing only whitespace as blank:
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;
}StringUtils.isNotBlank() is the negation of isBlank() :
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}StringUtils.isAnyBlank(...) returns true if any argument is blank:
StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, "foo") = true
StringUtils.isAnyBlank("", "bar") = true
StringUtils.isAnyBlank("bob", "") = true
StringUtils.isAnyBlank(" bob ", null) = true
StringUtils.isAnyBlank(" ", "bar") = true
StringUtils.isAnyBlank("foo", "bar") = false 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;
}StringUtils.isNoneBlank(...) returns true only when none of the arguments are blank:
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}Other StringUtils methods
For a complete list of utilities, refer to the official Apache Commons Lang documentation:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
The article also includes promotional content and QR‑code images for a “Top‑Level Architect” community, but the core technical material focuses on the StringUtils API.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.