判断字符串及对象是否为空的方法总结

判断字符串及对象是否为空的方法总结

public class Test1 {
    public static void main(String[] args){
        String s = "";
        /* 如果是null或者元素个数为0,返回true;并且判断其中字符是否包含空白符(空格、tab键、换行符) */
        System.out.println(org.apache.commons.lang.StringUtils.isBlank(s));
        /* 就是上面这个方法取反 */
        System.out.println(org.apache.commons.lang.StringUtils.isNotBlank(s));

        /* 如果是null或者元素个数为0,返回true */
        System.out.println(org.apache.commons.lang.StringUtils.isEmpty(s));
        /* 就是上面这个方法取反 */
        System.out.println(org.apache.commons.lang.StringUtils.isNotEmpty(s));

        /* 如果是null或者元素内容等于"",返回true */
        System.out.println(org.springframework.util.StringUtils.isEmpty(s));
        /* 如果不是是null并且元素个数不为0,返回true */
        System.out.println(org.springframework.util.StringUtils.hasLength(s));

        org.springframework.util.Assert.isNull(s,"s不是null抛异常");
        org.springframework.util.Assert.notNull(s,"s是null抛异常");

    }
}

 

posted @ 2019-01-10 11:28  唐胜伟  阅读(1109)  评论(0编辑  收藏  举报