IsEmpty和isBlank区别

  1. 详细测试结论如下
  • isEmpty ----》会把空格当成元素
  • isBlank ----》相反

class Test001 {
    static void t01() {
        String A = "  ";
        String B = "";
        String C = null;

        // 长度
        System.out.println(A.length());

        // 测试 isEmpty
        /* 从效果来看,当A="",字符中有空格时,IsEmpty是算他不为空的*/
        System.out.println("======测试 isEmpty======");
        System.out.println(StringUtils.isEmpty(A)); //会把空格当成元素
        System.out.println(StringUtils.isEmpty(B));
        System.out.println(StringUtils.isEmpty(C));

        // 测试 isBlank
        System.out.println("======测试 isBlank======");
        System.out.println(StringUtils.isBlank(A)); //不会把空格当成元素
        System.out.println(StringUtils.isBlank(B));
        System.out.println(StringUtils.isBlank(C));

        /* TODO 结论: isEmpty 等价于 str == null || str.length == 0;isBlank  等价于 str == null || str.length == 0 || str.trim().length == 0*/
        /* TODO 结论02: isNotEmpty和isNotBlank 结果相反*/

        /* 输出:
        2
                ======测试 isEmpty======
        false
        true
        true
                ======测试 isBlank======
        true
        true
        true

        Process finished with exit code 0*/

    }
}
原文地址:https://www.cnblogs.com/Twittery/p/14434994.html