SOP.Java工具集
2018-10-31 本文已影响30人
InitialX
- 判断字符串是否为空 StringUtils
- 用法:
import org.springframework.util.StringUtils
StringUtils.isEmpty(Object str);
----------------------------
- 源码:
public static boolean isEmpty(Object str) {
return (str == null || "".equals(str));
}
- 判断对象是否为空 ObjectUtils
- 用法:
import org.springframework.util.ObjectUtils
ObjectUtils.isEmpty(Object object);
- 源码:
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj instanceof Collection) {
return ((Collection) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map) obj).isEmpty();
}
// else
return false;
}
- 将字符串转换为字符串数组
如:String str = "a,b,c";
String[] newStr = str.split(",");
结果:
newStr[0] = "a" ;
newStr[1] = "b" ;
newStr[2] = "c" ;