总结的方法为apache的字符串工具类:
org.apache.commons.lang3.StringUtils
先说明空的两个容易混淆的概念,empty代表是空或者null,指的是:null或者"",引号中没有任何内容视为空。
blank在empty基础上增强,除了以上内容,空字符串也会被视为空,例如" "。
下面说各个方法:
isEmpty:当null和""的时候认为是空,返回true。判断也仅仅是判断是否为null和字符长度是否为0。
/**
* <p>Checks if a CharSequence is empty ("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* <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;
}
isBlank:在empty判断基础上增强,会循环字符串判断,如果都是空白符,返回true。
/**
* <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*
* @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;
}
这是empty和blank源码里的实现,从名字来讲也容易区分,empty为空,意为容器本身为空。blank原意为空白,意为容器内都是空白。
isNotEmpty和isNotBlank是分别对isEmpty和isBlank取反,不多说。
isNoneEmpty和isNoneBlank是分别对isAnyEmpty和isAnyBlank取反。
isAnyEmpty:与isEmpty不同的是,它的参数为可变参数,即可以判断多个参数,其中只要一个满足empty条件即为true。
/**
* <p>Checks if any one of the CharSequences are empty ("") or null.</p>
*
* <pre>
* 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
* </pre>
*
* @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;
}
isAnyBlank:与isBlank不同的是,它的参数为可变参数,即可以判断多个参数,其中只要一个满足blank条件即为true。
/**
* <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
*
* <pre>
* 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
* </pre>
*
* @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;
}