Mastering Apache Commons Lang StringUtils: When to Use isEmpty, isBlank, and Their Variants
This article provides a comprehensive guide to Apache Commons Lang's StringUtils class, explaining the behavior of isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank methods with code examples, edge‑case results, and practical usage tips.
StringUtils.isEmpty()
Checks whether a CharSequence is null or has a length of zero. A string containing only a space character is **not** considered empty.
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;
}StringUtils.isNotEmpty()
Returns the logical negation of isEmpty(), i.e., the value is not null and its length is greater than zero.
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}StringUtils.isAnyEmpty()
Evaluates to true if **any** of the supplied CharSequence arguments is null or empty.
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;
}StringUtils.isNoneEmpty()
Returns true only when **all** supplied arguments are non‑null and non‑empty; it is equivalent to !isAnyEmpty(css).
public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}StringUtils.isBlank()
Checks whether a CharSequence 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 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()
Logical negation of isBlank(); returns true when the input contains at least one non‑whitespace character.
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}StringUtils.isAnyBlank()
Evaluates to true if **any** of the provided CharSequence arguments is 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 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 **all** supplied arguments are non‑null, non‑empty, and contain at least one non‑whitespace character; equivalent to !isAnyBlank(css).
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}For a complete reference, consult the official Apache Commons Lang API documentation:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow 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.
