Spring中的isEmpty:判断str是否为null或是否为空字符串。
public static boolean isEmpty(@Nullable Object str) {
return str == null || "".equals(str);
}
commons中的isEmpty:isEmpty() 方法没有忽略空格,是以是否为空和是否存在为判断依据
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
但commons中又补充了isBlank方法:isBlank() 方法增加了字符串为空格、制表符的判断。即isBlank()的判断范围更大,它在isEmpty()方法的基础上,包括了空字符的判断。在实际开发中,isBlank()方法更加常用
public static boolean isBlank(String str) {
int strLen;
if (str != null && (strLen = str.length()) != 0) {
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
很显然,spring中的isEmpty可以接收Object类型的参数,而commons中的isEmpty只能接受String,日常开发中我们对实体类的id字段,age字段等大多使用包装类Integer,如果使用commons中的isEmpty判断在编写时会爆红。
Spring的isEmpty方法用于判断对象是否为null或字符串是否为空,而commons的isEmpty仅判断字符串是否为null或长度为0。此外,commons提供了isBlank方法,考虑了空格和制表符的情况,更适用于实际开发中的空白字符检查。Spring的方法接受Object参数,适合处理各种类型,而commons的方法限定为String类型。
439

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



