SOP.Java工具集

2018-10-31  本文已影响30人  InitialX
- 用法:
import org.springframework.util.StringUtils

StringUtils.isEmpty(Object str);

----------------------------
- 源码:
public static boolean isEmpty(Object str) {
        return (str == null || "".equals(str));
}
- 用法:
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" ;
上一篇下一篇

猜你喜欢

热点阅读