StringUtils—非空判断
1.StringUtils.isEmpty(String str)
isEmpty源码如下:
// Empty checks
//-----------------------------------------------------------------------
/**
* <p>Checks if a String 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 String.
* That functionality is available in isBlank().</p>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
即:
isEmpty(null)------true
isEmpty("")------true
isEmpty(" ")------false
2.StringUtils.isBlank(String str)
isBlank源码如下:
/**
* <p>Checks if a String 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 str the String to check, may be null
* @return <code>true</code> if the String is null, empty or whitespace
* @since 2.0
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
即:
isBlank(null)------true
isBlank("")------true
isBlank(" “)------true
isBlank(”\t \n \f \r")------true //制表符、换行符、换页符和回车符
本文介绍了StringUtils中的两个方法:isEmpty和isBlank,用于字符串的非空和空白判断。isEmpty不仅检查是否为null,还判断字符串是否为空;isBlank除了检查空字符串,还会考虑包含空白字符的情况。
586

被折叠的 条评论
为什么被折叠?



