Mastering Apache Commons StringUtils: When to Use isEmpty, isBlank, and More
This article explains the behavior and usage of Apache Commons Lang's StringUtils methods—including isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank—providing clear descriptions, code examples, and practical tips for Java developers.
isEmpty series
StringUtils.isEmpty()
Checks whether a CharSequence is null or has a length of zero. Note that a single space character is not considered empty.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = falseStringUtils.isNotEmpty()
Returns the opposite of isEmpty; true when the CharSequence is not null and not empty.
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}StringUtils.isAnyEmpty()
Returns true if any of the supplied CharSequences 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") = falseStringUtils.isNoneEmpty()
Returns true only when all supplied CharSequences are neither null nor empty (equivalent to !isAnyEmpty).
public static boolean isNoneEmpty(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return false;
}
for (final CharSequence cs : css) {
if (isEmpty(cs)) {
return false;
}
}
return true;
}isBlank series
StringUtils.isBlank()
Checks whether a CharSequence is null, empty, or contains only whitespace characters.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = falseStringUtils.isNotBlank()
Returns the opposite of isBlank; true when the CharSequence has non‑whitespace characters.
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}StringUtils.isAnyBlank()
Returns true if any of the supplied CharSequences 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") = falseStringUtils.isNoneBlank()
Returns true only when none of the supplied CharSequences are null, empty, or whitespace‑only (equivalent to !isAnyBlank).
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}For a complete reference, see the official Apache Commons Lang documentation.
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
