Exploring Apache Commons Lang StringUtils: isEmpty, isBlank, and Related Methods
This article provides a comprehensive overview of Apache Commons Lang's StringUtils class, explaining the behavior of isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank methods with code examples and a summary of additional useful utilities.
The author introduces the org.apache.commons.lang3.StringUtils utility class from Apache Commons Lang and walks through its various "is" methods for checking emptiness and blankness of strings.
StringUtils.isEmpty()
Demonstrates that StringUtils.isEmpty(null) and StringUtils.isEmpty("") return true , while a string containing only a space ( " " ) returns false . A code snippet shows the implementation:
/**
* NOTE: This method changed in Lang version 2.0.
* It no longer trims the CharSequence.
* That functionality is available in isBlank().
*
* @param cs the CharSequence to check, may be null
* @return true if the CharSequence is empty or null
*/
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}StringUtils.isNotEmpty()
Simply the negation of isEmpty() ( !isEmpty(cs) ).
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") = false
StringUtils.isNoneEmpty()
Returns true only when all supplied CharSequences are non‑empty; effectively !isAnyEmpty(css) .
public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}StringUtils.isBlank()
Considers null , empty string, and strings containing only whitespace as blank.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isNotBlank()
The negation of isBlank() ( !isBlank(cs) ).
StringUtils.isAnyBlank()
Returns true if any supplied CharSequence is blank (null, empty, or whitespace only).
StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank("bob", "") = 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 supplied CharSequences are blank; implemented as !isAnyBlank(css) .
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}Other Useful StringUtils Methods
The article lists additional methods such as Trim/Strip, Equals/Compare, startsWith, endsWith, IndexOf, Substring, Split/Join, Replace, AppendIfMissing, UpperCase/LowerCase, CountMatches, LevenshteinDistance, etc., with brief English and Chinese explanations. For full details, refer to the official documentation: Apache Commons Lang StringUtils Javadoc .
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
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.