StringUtils里面的 isEmpty方法和isBlank方法的区别:
public static boolean isEmpty(String str) {
// 判断字符串是否为空或长度为0
return str == null || str.length() == 0;
}
// 比isEmpty多了空字符串的校验
public static boolean isBlank(String str) {
int strLen;
// 判断字符串是否为空或长度为是否为0
if (str != null && (strLen = str.length()) != 0) {
// 如果字符串不为空,且长度不为0,进行循环遍历
for(int i = 0; i < strLen; ++i) {
// 如果字符串指定位置的值不为空白字符,返回false;否则返回true
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
测试:
public static void main(String[] args) {
System.out.println(StringUtils.isBlank("")); // true
System.out.println(StringUtils.isBlank(" ")); // true
System.out.println(StringUtils.isBlank(" ")); // true
System.out.println(StringUtils.isBlank(null)); // true
System.out.println(StringUtils.isNotBlank("")); // false
System.out.println(StringUtils.isNotBlank(" ")); // false
System.out.println(StringUtils.isNotBlank(" ")); // false
System.out.println(StringUtils.isNotBlank(null)); // false
System.out.println("============================================");
System.out.println(StringUtils.isEmpty("")); // true
System.out.println(StringUtils.isEmpty(" ")); // false
System.out.println(StringUtils.isEmpty(" ")); // false
System.out.println(StringUtils.isEmpty(null)); // true
System.out.println(StringUtils.isNotEmpty("")); // false
System.out.println(StringUtils.isNotEmpty(" ")); // true
System.out.println(StringUtils.isNotEmpty(" ")); // true
System.out.println(StringUtils.isNotEmpty(null)); // false
}