栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

String中常用的API(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

String中常用的API(一)

学习整理之String中常用的API(一)

(1)boolean isEmpty():字符串是否为空

    @Test
    public void test1(){
        String s1 = new String("");
        boolean b1 = s1.isEmpty();
        System.out.println(b1);//true

        String s2 = new String("123");
        boolean b2 = s2.isEmpty();
        System.out.println(b2);//false
    }

(2)String concat(xx):拼接,等价于+

    @Test
    public void test2(){
        String s1 = new String("abc");
        String s2 = new String("123");
        String s3 = s1.concat(s2);
        System.out.println(s3);//abc123
    }

(3)int length():返回字符串的长度

 @Test
    public void test3(){
        String s1 = new String("abc");
        int length = s1.length();
        System.out.println(length);//3
    }

(4)boolean equals(Object obj):比较字符串是否相等,区分大小写

 @Test
    public void test4(){
        String s1 = new String("abc");
        String s2 = new String("abc");
        String s3 = new String("ABC");
        String s4 = new String("123");

        boolean b1 = s1.equals(s2);
        System.out.println(b1);//true

        boolean b2 = s1.equals(s3);
        System.out.println(b2);//false

        boolean b3 = s1.equals(s4);
        System.out.println(b3);//false
    }

(5)boolean equalsIgnoreCase(Object obj):比较字符串是否相等,不区分大小写

    @Test
    public void test5(){
        String s1 = new String("abc");
        String s2 = new String("abc");
        String s3 = new String("ABC");
        String s4 = new String("123");

        boolean b1 = s1.equalsIgnoreCase(s2);
        System.out.println(b1);//true

        boolean b2 = s1.equalsIgnoreCase(s3);
        System.out.println(b2);//true

        boolean b3 = s1.equalsIgnoreCase(s4);
        System.out.println(b3);//false
    }

(6)int compareTo(String other):比较字符串大小,区分大小写,按照Unicode编码值比较大小

    @Test
    public void test6(){
        String s1 = new String("abc");
        String s2 = new String("ABC");
        String s3 = new String("bcd");
        String s4 = new String("狗吉");

        int i1 = s1.compareTo(s2);
        System.out.println(i1);//32

        int i2 = s2.compareTo(s3);
        System.out.println(i2);//-33

        int i3 = s1.compareTo(s3);
        System.out.println(i3);//-1

        int i4 = s1.compareTo(s4);
        System.out.println(i4);//-29302       
    }

(7)int compareToIgnoreCase(String other):比较字符串大小,不区分大小写

 @Test
    public void test7(){
        String s1 = new String("abc");
        String s2 = new String("ABC");
        String s3 = new String("bcd");
        String s4 = new String("狗吉");

        int i1 = s1.compareToIgnoreCase(s2);
        System.out.println(i1);//0

        int i2 = s2.compareToIgnoreCase(s3);
        System.out.println(i2);//-1

        int i3 = s1.compareToIgnoreCase(s3);
        System.out.println(i3);//-1

        int i4 = s1.compareToIgnoreCase(s4);
        System.out.println(i4);//-29302
    }

(8)String toLowerCase():将字符串中大写字母转为小写

    @Test
    public void test8(){
        String s1 = new String("AbcDefG");
        String s = s1.toLowerCase();
        System.out.println(s);//abcdefg
    }

(9)String toUpperCase():将字符串中小写字母转为大写

    @Test
    public void test9(){
        String s1 = new String("AbcDefG");
        String s = s1.toUpperCase();
        System.out.println(s);//ABCDEFG
    }

(10)String trim():去掉字符串前后空白符

@Test
    public void test10(){
        String s1 = new String("  abc  ");
        String trim1 = s1.trim();
        System.out.println(trim1);//abc

        String s2 = new String("  狗吉  ");
        String trim2 = s2.trim();
        System.out.println(trim2);//狗吉
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号