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);//狗吉
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zwjStart

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值