2020-12-24-Java-复习-63(lang3)

1.StringUtils

isEmpty isBlank区别
public class Test1 {
    public static void main(String[] args) {
        boolean empty = StringUtils.isEmpty("");
        boolean empty1 = StringUtils.isEmpty(" ");
        boolean empty2 = StringUtils.isEmpty(null);
        boolean blank = StringUtils.isBlank("");
        boolean blank1 = StringUtils.isBlank(" ");
        boolean blank2 = StringUtils.isBlank(null);
        System.out.println(empty);
        System.out.println(empty1);//false
        System.out.println(empty2);
        System.out.println(blank);
        System.out.println(blank1);
        System.out.println(blank2);
        //其他都为true
    }
}
trim strip deleteWhitespace 处理字符串
public class Test2 {
    public static void main(String[] args) {
        String trim = StringUtils.trim("    as  ");
        System.out.println(trim);//as
        String strip = StringUtils.strip(" abc ", null);
        System.out.println(strip);//abc
        String strip1 = StringUtils.strip(" ABCD", "DB");
        System.out.println(strip1);//  ABC
        String strip2 = StringUtils.strip(" ABCD", "DBC");
        System.out.println(strip2);// A
        //去除字符串中的空格
        String s = StringUtils.deleteWhitespace("  mm  mm  dd  ");
        System.out.println(s);//mmmmdd
    }
}

字符串比对方法,是比较实用的方法之一,两个比较的字符串都能为空,不会报空指针异常

public class Test3 {
    public static void main(String[] args) {
        boolean equals = StringUtils.equals(null, "");
        boolean equals1 = StringUtils.equals(" ", "  ");
        boolean equals2 = StringUtils.equals(" ", " ");
        System.out.println(equals);//false
        System.out.println(equals1);//false
        System.out.println(equals2);//true
        //忽视大小写
        boolean b = StringUtils.endsWithIgnoreCase("A", "a");
        System.out.println(b);//true

    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容