StringUtils中的isNotEmpty、isEmpty和isNotBlank、isBlank的区别
依赖
isNotEmpty和isEmpty
isNotEmpty 判断字符串 不为空且长度不为0
isEmpty 判断字符串 为空或长度为0
例子
StringUtils.isEmpty(null) = true //str==null
StringUtils.isEmpty("") = true //str.length()==0
StringUtils.isEmpty(" ") = false //str.length()==1
StringUtils.isEmpty("bob") = false //str.length()==3
StringUtils.isEmpty(" bob ") = false //str.length()==5
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
底层处理
public static boolean isNotEmpty (CharSequence cs){
return !isEmpty(cs);
}
public static boolean isEmpty (CharSequence cs){
return cs == null || cs.length() == 0;
}
isNotBlank和isBlank
isNotBlank 判断字符串 不为空且长度不为0且不由空白符构成
(whitespace,空白符包含:空格、tab键、换行符;例如:'\n'、' '、'\t'等)
isBlank 判断字符串 为空或长度为0或由空白符(whitespace) 构成
例子
StringUtils.isBlank(null) = true //str==null
StringUtils.isBlank("") = true //str.length()==0
StringUtils.isBlank(" ") = true //空白符 空格
StringUtils.isBlank("\t \n \f \r") = true //空白符
StringUtils.isBlank("\b") = false //"\b"为单词边界符
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("\t \n \f \r") = false
StringUtils.isNotBlank("\b") = true
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true
底层处理
public static boolean isNotBlank (CharSequence cs){
return !isBlank(cs);
}
public static boolean isBlank (CharSequence cs){
int strLen;
if (cs != null && (strLen = cs.length()) != 0) {
for (int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
JAVA doc
//isEmpty
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since 3.2
*/
//isNotEmpty
/**
* Null-safe check if the specified collection is not empty.
* <p>
* Null returns false.
*
* @param coll the collection to check, may be null
* @return true if non-null and non-empty
* @since 3.2
*/
//isBlank
/**
* <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)
*/
//isNotBlank
/**
* <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
*
* <pre>
* StringUtils.isNotBlank(null) = false
* StringUtils.isNotBlank("") = false
* StringUtils.isNotBlank(" ") = false
* StringUtils.isNotBlank("bob") = true
* StringUtils.isNotBlank(" bob ") = true
* </pre>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is
* not empty and not null and not whitespace
* @since 2.0
* @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
*/