Mastering Apache Commons Lang StringUtils: Hidden Empty/Blank Checks Explained
This article explores the lesser‑known Apache Commons Lang StringUtils methods such as isAnyEmpty, isNoneEmpty, isAnyBlank and isNoneBlank, showing how they differ from the classic isEmpty/isBlank checks with clear code examples and usage guidelines.
Overview
The org.apache.commons.lang3.StringUtils class in Apache Commons Lang offers a set of null‑safe methods for checking whether a CharSequence is empty or blank. In addition to the classic isEmpty and isBlank, the library provides multi‑argument variants isAnyEmpty, isNoneEmpty, isAnyBlank and isNoneBlank that evaluate collections of strings in a single call.
isEmpty Series
StringUtils.isEmpty(CharSequence cs) returns true when the argument is null or has a length of zero. A single space character is **not** considered empty.
/**
* Returns true if the CharSequence is null or empty.
* Since 3.0 the signature accepts CharSequence.
*/
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}Typical results:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isNotEmpty(CharSequence cs) is the logical negation of isEmpty:
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}StringUtils.isAnyEmpty(CharSequence... css) returns true if **any** supplied element is null or empty.
StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, "foo") = true
StringUtils.isAnyEmpty("", "bar") = true
StringUtils.isAnyEmpty("bob", "") = true
StringUtils.isAnyEmpty(" ", "bar") = false
StringUtils.isNoneEmpty(CharSequence... css) returns true only when **all** arguments are non‑null and non‑empty; it is implemented as the negation of isAnyEmpty:
/**
* Checks that none of the CharSequences are empty or null.
*/
public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}isBlank Series
StringUtils.isBlank(CharSequence cs) treats null, the empty string, and strings consisting solely of whitespace as blank.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isNotBlank(CharSequence cs) is the negation of isBlank:
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}StringUtils.isAnyBlank(CharSequence... css) returns true if **any** argument is blank (null, empty, or whitespace‑only). The implementation checks for an empty array first, then iterates over the arguments.
/**
* Returns true if any CharSequence is blank.
*/
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(CharSequence... css) returns true only when **all** arguments are non‑blank; it is the logical negation of isAnyBlank:
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}Reference
Full Javadoc for StringUtils is available at: 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.
