jar包:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
1、StringUtils判空方法
源码
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;
}
}
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
String strNull = null;
System.out.println("strNull isBlank:" + StringUtils.isBlank(strNull) + ",isEmpty:" + StringUtils.isEmpty(strNull));
String str = "";
System.out.println("str\"\" isBlank:" + StringUtils.isBlank(str) + ",isEmpty:" + StringUtils.isEmpty(str));
String strKongge = " ";
System.out.println("strKongge isBlank:" + StringUtils.isBlank(strKongge) + ",isEmpty:" + StringUtils.isEmpty(strKongge));
String strEmPTY = StringUtils.EMPTY;
System.out.println("strEmPTY isBlank:" + StringUtils.isBlank(strEmPTY) + ",isEmpty:" + StringUtils.isEmpty(strEmPTY));
String strSpe1 = "\t";
System.out.println("strSpe1\\t isBlank:" + StringUtils.isBlank(strSpe1) + ",isEmpty:" + StringUtils.isEmpty(strSpe1));
String strSpe2 = "\n";
System.out.println("strSpe2\\n isBlank:" + StringUtils.isBlank(strSpe2) + ",isEmpty:" + StringUtils.isEmpty(strSpe2));
String strSpe3 = "\n\t";
System.out.println("strSpe3\\n\\t isBlank:" + StringUtils.isBlank(strSpe3) + ",isEmpty:" + StringUtils.isEmpty(strSpe3));
strNull isBlank:true,isEmpty:true
str"" isBlank:true,isEmpty:true
strKongge isBlank:true,isEmpty:false
strEmPTY isBlank:true,isEmpty:true
strSpe1\t isBlank:true,isEmpty:false
strSpe2\n isBlank:true,isEmpty:false
strSpe3\n\t isBlank:true,isEmpty:false
2、集合判空CollectionUtils.isEmpty
jar包:
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}