• String类中常用的方法


    @Test
        public void demo(){
    
            // 以下为String中的常用的方法及注释, 最常用的注释前有**标注
            String s = "abcdefg123456";
    
            // 返回下标对应的ASCII码
            int codePointAt = s.codePointAt(7);
            // **返回字符串s和字符串Q的,第一个不相等字符的ASCII码差值,如果查找结束发现都相等,则返回两个字符串的长度差值
            int compareTo = s.compareTo("Q");
            // 返回下标前一位对应的ASCII码
            int codePointBefore = s.codePointBefore(7);
            // 返回代码点长度
            int codePointCount = s.codePointCount(4, 7);
            // 返回从index=4处偏移到codePointOffSet=7之后的索引
            int offsetByCodePoints = s.offsetByCodePoints(4, 7);
            // 将字符串变成字节数组
            byte[] getBytes = s.getBytes();
            // **将字符串变成字符数组
            char[] toCharArray = s.toCharArray();
            // **返回指定索引出的字符
            char charAt = s.charAt(7);
            // **是否包含字符串"contains"
            boolean contains = s.contains("contains");
            // **将字符串s中的c全部替换为C
            String replaceAll = s.replaceAll("c", "C");
            // 将字符串中的1替换为一
            String replace = s.replace("1", "一");
            // **截取字符串s中的索引从0到3的字符
            String substring = s.substring(0, 3);
            // 截取字符串s中索引从4开始的字符
            String substring1 = s.substring(4);
            // 字符串s是否以6结尾
            boolean endsWith = s.endsWith("6");
            // 字符串是否以a开头
            boolean startsWith = s.startsWith("compareTo");
            // **将字符串s以字符c进行分割
            String[] split = s.split("c");
            // 将字符串s以字符c进行分割,分割成3份(如果最大能够分割到3份的话,如果最大不能分割到3份,则默认分割到最大份数)
            String[] split1 = s.split("c", 3);
            // 将字符串7890拼接到字符串s的后面,与+拼接字符串的区别是,+号左右的字符串都可以是null,而concat左右都不允许,否则会报空指针异常
            String concat = s.concat("7890");
            // 返回字符串是否相等,与equals区别,equals仅限于两个String类型字符串进行比较,而contentEquals可以比较类型为CharSequence的,
            // 而且如果比较的是StringBuffer还会加锁
            boolean contentEquals = s.contentEquals("contentEquals");
            // 比较两个字符串是否相等(忽略大小写),
            boolean equalsIgnoreCase = s.equalsIgnoreCase("Abcdefg123456");
            // **比较两个字符串是否相等(不忽略大小写)
            boolean equals = s.equals("contentEquals");
            // 判断字符串是否为空
            boolean empty = s.isEmpty();
            // **获取字符串的长度
            int length = s.length();
            // **返回指定字符在字符串s中的索引,如果找不到返回-1
            int indexOf = s.indexOf("一");
            // 将字符串全部转换为小写
            String toLowerCase = s.toLowerCase();
            // 将字符串全部转换为大写
            String toUpperCase = s.toUpperCase();
            // 去除字符串前后的空格
            String trim = "  abc   ".trim();
            // 将指定数量的字符串(s,7890,AAA)以固定格式(,)进行拼接
            String join = String.join(",",s, "7890", "AAA");
    
    
    
            System.out.println("codePointAt = " + codePointAt);
            System.out.println("compareTo = " + compareTo);
            System.out.println("codePointBefore = " + codePointBefore);
            System.out.println("codePointCount = " + codePointCount);
            System.out.println("offsetByCodePoints = " + offsetByCodePoints);
            System.out.println("getBytes = " + Arrays.toString(getBytes));
            System.out.println("toCharArray = " + Arrays.toString(toCharArray));
            System.out.println("charAt = " + charAt);
            System.out.println("contains = " + contains);
            System.out.println("replaceAll = " + replaceAll);
            System.out.println("replace = " + replace);
            System.out.println("substring = " + substring);
            System.out.println("substring1 = " + substring1);
            System.out.println("endsWith = " + endsWith);
            System.out.println("startsWith = " + startsWith);
            System.out.println("split = " + Arrays.toString(split));
            System.out.println("split1 = " + Arrays.toString(split1));
            System.out.println("concat = " + concat);
            System.out.println("contentEquals = " + contentEquals);
            System.out.println("equalsIgnoreCase = " + equalsIgnoreCase);
            System.out.println("indexOf = " + indexOf);
            System.out.println("toLowerCase = " + toLowerCase);
            System.out.println("toUpperCase = " + toUpperCase);
            System.out.println("join = " + join);
            System.out.println("equals = " + equals);
            System.out.println("empty = " + empty);
            System.out.println("length = " + length);
            System.out.println("trim = " + trim);
        }
    上述打印结果为:
    
    codePointAt = 49
    compareTo = 16
    codePointBefore = 103
    codePointCount = 3
    offsetByCodePoints = 11
    getBytes = [97, 98, 99, 100, 101, 102, 103, 49, 50, 51, 52, 53, 54]
    toCharArray = [a, b, c, d, e, f, g, 1, 2, 3, 4, 5, 6]
    charAt = 1
    contains = false
    replaceAll = abCdefg123456
    replace = abcdefg一23456
    substring = abc
    substring1 = efg123456
    endsWith = true
    startsWith = false
    split = [ab, defg123456]
    split1 = [ab, defg123456]
    concat = abcdefg1234567890
    contentEquals = false
    equalsIgnoreCase = true
    indexOf = -1
    toLowerCase = abcdefg123456
    toUpperCase = ABCDEFG123456
    join = abcdefg123456,7890,AAA
    equals = false
    empty = false
    length = 13
    trim = abc
    以上为个人总结记录使用,希望能够帮助其他人,如有不足或错误欢迎大家指正。
    
     
  • 相关阅读:
    PYQT4 : QSystemTrayIcon练习
    PyQt4 初试牛刀一
    PyQt4简单小demo
    PyQt5实时汇率查询
    PyQt:昨天今天明天表示方法
    mybatis拓展框架对比
    docker部署oracle
    docker部署Jenkins,以及在Jenkins中使用宿主机的docker/docker-compose命令
    Jersey 出现415 MediaType is not supported问题的原因
    [转]前端常见跨域解决方案(全)
  • 原文地址:https://www.cnblogs.com/kaifaxiaoliu/p/11980113.html
Copyright © 2020-2023  润新知