在日常开发中,用的最多的StringUtils工具类就是org.apache.commons.lang3.StringUtils。在这个工具类中有许多方法,如:isEmpty、isNotEmpty、isNotBlank、isBlank等常用的方法。除此之外,还有许多不常用的方法,如:isAnyEmpty、isNoneEmpty、isAnyBlank、isNoneBlank等。下面就来看看这些方法都是如何使用的。
一、StringUtils.isEmpty()
是否为空。可以看到" “空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isEmpty(” ")=false。
/**
*
* <p>NOTE: This method changed in Lang version 2.0.
* It no longer trims the CharSequence.
* That functionality is available in isBlank().</p>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is empty or null
* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
*/
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty(“test”) = false
StringUtils.isEmpty(" test") = false
二、StringUtils.isAnyEmpty()
是否有一个为空,只要有一个为空,就为true。
/**
* @param css the CharSequences to check, may be null or empty
* @return {@code true} if any of the CharSequences are empty or null
* @since 3.2
*/
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;
}
public static boolean isNoneEmpty(CharSequence... css) {
return !isAnyEmpty(css);
}
StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, “test”) = true
StringUtils.isAnyEmpty("", “test”) = true
StringUtils.isAnyEmpty(“test”, “”) = true
StringUtils.isAnyEmpty(" test ", null) = true
StringUtils.isAnyEmpty(" ", “test”) = false
StringUtils.isAnyEmpty(“test”, “test”) = false
三、StringUtils.isBlank()
是否为真空值(空格或者空值)。
/**
* <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is null, empty or whitespace
* @since 2.0
* @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
*/
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)) == false) {
return false;
}
}
return true;
}
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank(“test”) = false
StringUtils.isBlank(" test") = false
四、StringUtils.isAnyBlank()
是否包含任何真空值(包含空格或空值)。
/**
* <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
* @param css the CharSequences to check, may be null or empty
* @return {@code true} if any of the CharSequences are blank or null or whitespace only
* @since 3.2
*/
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;
}
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}
StringUtils.isNoneBlank(null) = false
StringUtils.isNoneBlank(null, “test”) = false
StringUtils.isNoneBlank(null, null) = false
StringUtils.isNoneBlank("", “test”) = false
StringUtils.isNoneBlank(“test”, “”) = false
StringUtils.isNoneBlank(" test", null) = false
StringUtils.isNoneBlank(" ", “test”) = false
StringUtils.isNoneBlank(“test”, “test”) = true
更多方法可以参考官方文档:StringUtils官方文档