String 类

一.基本特性
1.不可变性

  • String对象一旦创建,其内容不可修改(底层使用final char[]存储,JDK9+改为byte[])
  • 修改操作(如拼接、替换)会生成新对象,原对象不变
    2.创建方式
String s1 = "abc";          // 字面量(常量池)
String s2 = new String("abc"); // new对象(堆内存)
char[] arr = {'a','b','c'};
String s3 = new String(arr);  // 字符数组构造

深入理解创建:
在这里插入图片描述
‌3.常量池优化‌

  • 字面量创建的字符串会存入常量池,相同内容的对象复用同一内存地址
  • intern()方法可将堆中字符串手动加入常量池

在这里插入图片描述
二、核心方法

‌方法类型‌‌示例方法‌‌说明‌
‌比较‌equals() / compareTo()内容比较(区分大小写)/字典序比较
‌查找‌indexOf() / contains()返回子串位置或是否存在
‌截取与分割‌substring() / split()提取子串或按分隔符拆分
‌转换‌toUpperCase() / valueOf()大小写转换/其他类型转字符串
‌正则处理‌matches() / replaceAll()正则匹配与替换
//比较
    public static void main2(String[] args) {

        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = s1;
        System.out.println("=============");
        //==比较的是地址
        System.out.println(s1 == s2);//false
        System.out.println(s1 == s3);//true
        System.out.println(s2 == s3);//false
        System.out.println("========================");
        //equals比较的是字符
        System.out.println(s1.equals(s2));//true
        System.out.println(s1.equals(s3));//true
        System.out.println(s2.equals(s3));//true
    }

    public static void main3(String[] args) {
        String s1 = new String("aeF");
        String s2 = new String("aef");
        //比较字符串的大小
        System.out.println(s1.compareTo(s2));//s1和s2进行比较
        //1. 根据对应字符比较
        //2. 对应字符 无法比较出来 谁大 谁小 此时 就看长度
        System.out.println(s1.compareToIgnoreCase(s2));
    }
//查找

    public static void main4(String[] args) {
        String s = "ababcabcd";
        //倒着往回的 第一个ab
        int index = s.lastIndexOf("abc",4);
        System.out.println(index);

       /* int index = s.indexOf('l');//默认是从0位置开始找
        System.out.println(index);


        index = s.indexOf('l',3);
        System.out.println(index);

        index = s.indexOf("ll",3);//默认是从0位置开始找
        System.out.println(index);
*/

        /*for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            System.out.println(ch);
        }*/


        /*char ch = s.charAt(5);
        System.out.println(ch);*/
        /*int[] array = {1,2,3,4,5};
        System.out.println(array.length);*/
    }

//‌截取与分割‌
    public static void main9(String[] args) {
        //"name=zhangsan&age=10"
        String s = "name=zhangsan&age=10";
        //返回值  是数组
        String[] strings = s.split("&");//按照&分割
        for (int i = 0; i < strings.length; i++) {
            //strings[i] = name=zhangsan
            //strings[i] = age=10
            //System.out.println(strings[i]);
            String[] ss = strings[i].split("=");
            for (int j = 0; j < ss.length; j++) {
                System.out.print(ss[j]+" ");
            }
            System.out.println();
        }
    }

    public static void main13(String[] args) {
        String s = "abcdef";
        String s2 = s.substring(2,4);//[)从第二个开始截取到第四个结束不包含第四个
        System.out.println(s2);
    }
//‌转换‌
    public static void main6(String[] args) {
        String s = "hello";
        String s2 = s.toUpperCase();
        System.out.println(s2);

        //汉字不发生转化
        String s3 = "Hel汉字lo9";
        System.out.println(s3.toLowerCase());
    }
    public static void main5(String[] args) {
        //数值转换成String类型
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        /*String s4 = String.valueOf(new Student("gaobo",13));
        System.out.println(s4);*/

        //String类型转换成其他类型
        int data1 = Integer.parseInt("1234");
        double data2 = Double.parseDouble("12.34");
        System.out.println(data1+1);
        System.out.println(data2);
    }
   
//‌正则处理‌ 
    public static void main8(String[] args) {
        String s = "ababcabcd";
        String tmp = s.replace('a','k');
        System.out.println(tmp);

        tmp = s.replace("ab","uuu");
        System.out.println(tmp);

        tmp = s.replaceFirst("ab","ooo");//只转换第一个
        System.out.println(tmp);

        tmp = s.replaceAll("ab","ooo");
        System.out.println(tmp);
    }
    
    public static void main14(String[] args) {
        String s = "  abc d e f g    ";
        System.out.println(s);

        String s2 = s.trim();//只会去除  左右两边的空格
        System.out.println(s2);

    }

三、性能优化‌
1‌.拼接场景‌

  • 单次拼接:使用+(编译期优化)
  • 循环拼接:优先用StringBuilder(非线程安全)或StringBuffer(线程安全)

2‌.内存管理‌

  • 避免大字符串直接加载,采用分块处理(如文件逐行读取)
  • 复用常量池对象减少内存开销
 public static void main16(String[] args) {
        String s = "hello";
        s += " world";//s = s + ....
        System.out.println(s);
    }

    public static void main17(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("hello");//拼接
        stringBuilder.append(" world");
        String ret = stringBuilder.toString();
        System.out.println(ret);
    }
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐