首页 > 技术文章 > learning java 字符串常用操作

lianghong881018 2019-07-25 16:03 原文

        // 字符串索引取值
        var s = "12345678";
        System.out.println(s.charAt(0));

        // 字符串比较
        var s1 = "1234567890";
        var s2 = "12345678";
        var s3 = "1234567870";

        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));

        //字符串拼接
        var s4 = "allaboutscala";
        var s5 = ".org";

        System.out.println(s4.concat(s5));

        // 字符串前缀及后缀判断
        var s6 = "www.kernel.org";
        var s7 = "www";
        var s8 = "org";
        System.out.println(s6.startsWith(s7));
        System.out.println(s6.endsWith(s8));

        // 字符串拷贝
        char[] s9 = {'1','2','3','4'};
        var s10 = "abc";

        s10.getChars(0,2,s9,0);
        System.out.println(s9);
        //字符查找
        System.out.println(s6.indexOf('w'));
        System.out.println(s6.indexOf('w',6));

        System.out.println(s6.lastIndexOf('w'));

        System.out.println(s6.replace('w','W'));

        System.out.println(s6.length());

        //字符串截取
        System.out.println(s6.substring(4));

        System.out.println(s6.substring(4,6));

        System.out.println(s6.toUpperCase());
        System.out.println(s6.toLowerCase());

        System.out.println(s6.toCharArray());

output:

1
2
2
allaboutscala.org
true
true
ab34
0
-1
2
WWW.kernel.org
14
kernel.org
ke
WWW.KERNEL.ORG
www.kernel.org
www.kernel.org

 

推荐阅读