• String类


    String类常用的方法

    字符串构造

    String类的构造方法很多,常见的有以下的三种:

    //三种常用的构造方法
    public class Test {
        public static void main(String[] args) {
            //使用常量串构造
            String s1 = "hello world!";
            //直接new String对象构造
            String s2 = new String("hello world!");
            //使用字符串数组进行构造
            char[] array = {'h', 'e', 'l', 'l', 'o'};
            String s3 = new String(array);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    【注意】

    1. String是引用类型,内部并不储存字符串本身,在String类的实现原码中,String类的实例变量如下:
      在这里插入图片描述
      在JDK1.8中, 字符串实际保存在char类型的数组中
      在这里插入图片描述
    2. 在Java中“ ”引起来的也是String类型的对象

    System.out.printlen(“hello”.lenth());
    //这个 hello 也是String类型

    String对象的比较

    我们如何比较字符串的大小呢?Java中共提供了四种方法:

    1. ==比较是否引用同一对象
    //==比较是否引用同一个对象
    public class Test {
        public static void main(String[] args) {
            String s1 = new String("nihao");
            String s2 = new String("nihao");
            String s3 = s1;
            System.out.println(s1 == s2);
            System.out.println(s1 == s3);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述
    这里s1和s2引用的是不同的对象,使用”==“比较时候,地址不同,所以不相等。而s1和s3是引用的同一对象,所以相同。
    2. boolean equals(Object anObject)方法:按照英文字母顺序查找
    String类中重写了父类 Object中equals方法,Object中equals默认按照 == 比较,String重写equals方法后,按照如下规则比较:
    在这里插入图片描述
    比如”hello"与“hello world”比较,先比较长度,长度不相等,返回false ; 长度如果相等逐个字母进行比较,不相同返回false;全部相同,返回true
    3. int compareTo(String s)方法按照英文顺序比较
    与equals不同的是,equals返回值是boolean类型,而compareTo返回的是int类型,比较的方法如下:

    • 先按照字母的顺序比较,如果出现不相同的字母,直接返回两个字符大小的差值;
    • 如果前K个字符相同(K为两字符的最小长度),返回值两个字符串长度差值。
    // int compareTo(String s) 方法
    public class Test {
        public static void main(String[] args) {
            String s1 = "xiaoming";
            String s2 = "xiaohong";
            String s3 = "miaomiao";
            String s4 = "xiao";
            System.out.println(s1.compareTo(s2));//前几个字母相等,返回m-h的值
            System.out.println(s1.compareTo(s3));//返回x-m的值
            System.out.println(s1.compareTo(s4));//前几个字母相等,返回字母数量的差值
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. int compareTolgnoreCase(String str)方法:compareTo方式相同,但是忽略大小写的比较。
    //int compareTolgnoreCase(String str)方法
    public class Test {
        public static void main(String[] args) {
            String s1 = "XIAOMING";
            String s2 = "xiaoming";
            String s3 = "mingming";
            System.out.println(s1.compareToIgnoreCase(s2));//输出为0
            System.out.println(s1.compareToIgnoreCase(s3));//首字母大小不一样,返回首字母大小的差值
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    字符串查找

    字符串查找也是字符串中常见的操作,String类提供的常见查找方法:

    方法功能
    char chaAt(int index)返回值index位置上字符,如果index为负数或者越界,将抛出IndexOutOfBountsException异常
    int indexof(int ch)返回ch第一次出现出现的位置,没有返回-1
    int indexOf(int ch, int fromIndex)冲fromIndex位置开始找ch第一次出现的位置,没有返回-1
    int indexof(Sting str)返回str第一次出现的位置,没有返回-1
    int indexof(Sting str, int fromIndex)从fromIndex位置考试找str第一次出现的位置,没有返回-1
    int lastIndexof(int ch)从后往前找,返回ch第一次出现的位置,没有返回-1
    int lastIndexof(int ch, int fromIndex)从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1
    int lastIndexOf(String str)从后往前,返回str第一次出现的位置,没有返回-1
    int lastIndexOf(String str, int fromIndex)从fromIndex位置开始找,从后往前找返回第一个str的位置,没有返回-1
    //字符、字符串查找相关函数
    public class Test {
        public static void main(String[] args) {
            String s = "abcdcba";
            System.out.println(s.charAt(0));//找到第一个字符,查看是什么
            System.out.println(s.indexOf('c'));//从前往后找第一个c
            System.out.println(s.indexOf('c', 3));//从第四个字母开始找,出现c的位置
            System.out.println(s.indexOf("cba"));//从前往后找找第一个cba
            System.out.println(s.lastIndexOf('c'));//从后向前找第一个c
            System.out.println(s.lastIndexOf('c', 3));//从第四个位置开始,找第一个c
            System.out.println(s.lastIndexOf("cb"));//从后向前,找第一次出现cb的位置
            System.out.println(s.lastIndexOf("cb", 5));//从第五个位置从后向前找cb
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    转化

    1. 数值和字符串转化
    //数值和字符串转化
    class Student {
        String s;
        int age;
    
        public Student(String s, int age) {
            this.s = s;
            this.age = age;
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            String s1 = String.valueOf(123);
            String s2 = String.valueOf(3.1415926);
            String s3 = String.valueOf(true);
            String s4 = String.valueOf(new Student("xiaoming", 18));
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(s4);
            //字符串转化为数字
            int a = Integer.parseInt(s1);
            double b = Double.parseDouble(s2);
            System.out.println(a);
            System.out.println(b);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    1. 大小写转化
    //大小写转化
    public class Test {
        public static void main(String[] args) {
            String s1 = "hello WORLD!";
            String s2 = "nihao 2022";
            System.out.println(s1.toLowerCase());
            System.out.println(s1.toUpperCase());
            System.out.println(s2.toUpperCase());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 字符串转数组
    //字符串转数组
    public class Test {
        public static void main(String[] args) {
            String s = "hello";
            char[] ch = s.toCharArray();
            for (int i = 0; i < ch.length; i++) {
                System.out.print(ch[i]);
            }
            System.out.println();
            String s2 = new String(s);
            System.out.println(s2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 格式化
    //格式化
    public class Test {
        public static void main(String[] args) {
            String s = String.format("%d-%d-%d",2022,9,14);
            System.out.println(s);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    字符串替换

    使用一个新的字符串替换掉已有的字符串数据

    String replaceAll(String regex, String replacement)//替换所有的
    String replaceFirst(String regex, String replacement)//替换第一个 regex

    //字符串替代
    public class Test {
        public static void main(String[] args) {
            String s = "hello world!";
            System.out.println(s.replaceAll("l", "-"));
            System.out.println(s.replaceFirst("l","-"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述
    这里我们要注意,由于字符串是不可变对象,替换并不修改当前的字符串,而是创建一个新的字符串

    字符串拆分

    将一个完整的字符串按照指定的分隔符进行查分成若干个子字符串。

    String[] split(String regex) //将字符串根据regex进行全部拆分
    String[] split(String regex, int limit);//将字符串以指定的格式进行差分

    //字符串拆分
    public class Test {
        public static void main(String[] args) {
            String str = "hello world, hello baobo!";
            String[] result = str.split("ll");
            for (String s: result) {
                System.out.println(s);
            }
            System.out.println("===========================");
    
            String[] result1 = str.split(" ", 2);//2代表分成两端
            for (String s: result1) {
                System.out.println(s);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    【注意】有些特殊字符作为分隔符时,需要加上转义字符

    1. 字符“|” “*” “+”都加上转移字符,前面加上“\"
    2. 如果是 ““,那么需要写成 “\\”
    3. 如果一个字符串中有多个字符串,可以使用 “|” 作为连字符
    //特殊字符作为分割字符
    public class Test {
        public static void main(String[] args) {
            String s1 = "zhansan*age=18";
            String[] s2 = s1.split("\\*");
            System.out.println(s2[0]);
            System.out.println(s2[1]);
            System.out.println("=============================");
            String s3 = "d\\xuexi\\wuli";
            String[] s4 = s3.split("\\\\");
            System.out.println(s4[0]);
            System.out.println(s4[1]);
            System.out.println(s4[2]);
            System.out.println("=============================");
            String[] s5 = s1.split("\\*|=");
            System.out.println(s5[0]);
            System.out.println(s5[1]);
            System.out.println(s5[2]);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    字符串截取

    从一个完整的字符串中截取部分内容,

    String substring(int beginIndex);//从某一下标处开始截取
    String substring(int beginIndex,int endIndex);//从 begin下标处截取到end下标出

    //字符串截取
    public class Test {
        public static void main(String[] args) {
            String s1 = "nihao xiaoming";
            System.out.println(s1.substring(6));
            System.out.println(s1.substring(1,6));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述
    【注意事项】

    1. 索引从0开始;
    2. ***我们写的这个去见都是前开后壁的,比如这里,包含1,不包含6下标的字母

    其他操作方法

    方法功能
    String trim()去掉字符串中的左右空格,保留中间空格。
    String toUpperCase()字符串大写
    String toLowerCase()字符串转小写
    public class Test {
        public static void main(String[] args) {
            String s = " Hello world! ";
            System.out.println(s.trim());//trim去掉字符串两边的空格
            System.out.println(s.toUpperCase());//将字符按转化为大写
            System.out.println(s.toLowerCase());//将字串转化为小写
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    字符串的不可变性

    String是一种不可变独享,字符串中的内容是不能改变的。字符串不可改变的愿意:

    1. ***String类在设计时就是不可改变的,String类实现描述中已经说明。
      在这里插入图片描述
      在这里插入图片描述
      String类中的字符串际保存在内部维护的value字符数组中
    2. 所有设计到可能改变字符串内容的操作实际上都是新建一个对象,改变的是新的对象。
      在这里插入图片描述
      String类无法改变的真正原因是,final修饰了内部的value,同时value被private修饰,且没有提供value的改变的方法,如果提供value改变方法,字符串是可以改变的。String类中的final只是表示它无法被继承。

    为什么String要被设计成不可改变的呢?(选学)

    1. 方便实现字符串对象池。如果String可变,那么对象池就可能需要考虑写时拷贝的问题;
    2. 不可变对象的线程是安全的;
    3. 不可变对象更方便缓存hash code,作为key时可以更高效的保存到HashMap中。

    字符串修改

    【注意】尽量避免直接对String类型对象进行修改,因为String类是不可能修改的,所有的修改都会创建新对象,效率非常低

    //字符串避免频繁修改
    public class Test {
        public static void main(String[] args) {
            String s = "hello";
            s += "world";
            System.out.println(s);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    像这种方式,其实中间回创建很多的临时对象,效率很低

    StringBuilder和StringBuffer

    StringBuder的介绍

    由于String的不可更改特性,为了方便字符串的修改,Java中提供了StringBuider和StringBuffer类。

    //StringBuilder的一些方法
    public class Test {
        public static void main(String[] args) {
            StringBuilder s1 = new StringBuilder("Hello");
            StringBuilder s2 = s1;
            s1.append(' ');//追加字符
            s1.append("world");//追加字符串
            s1.append(123);//追加数字
            System.out.println(s1);//在原本的基础上进行追加
            System.out.println(s1 == s2);//true
            System.out.println(s1.charAt(0));//获取0下标的字符
            System.out.println(s1.length());//获取长度
            System.out.println(s1.capacity());//获取底层数组总大小
            s1.setCharAt(0,'h');//改变0位置的字符
            s1.insert(0, "nihao!");//在0下标前面,追加字符串
            System.out.println(s1);
            System.out.println(s1.indexOf("hello"));//获取第一次hello出现的位置
            System.out.println(s1.lastIndexOf("hello"));//获取从前往后数第一次出现hello的位置
            s1.deleteCharAt(0);//删除首字符
            s1.delete(0,5);//删除【0,6)范围内的字符
            System.out.println(s1.substring(0, 6));//截取【0,6)范围内的字符
            s1.reverse();//字符串逆转
            System.out.println(s1.toString());//以字符串形式输出
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述
    StringBuilder的内容可以被改变(在原来的内容上进行修改);
    【注意】String 和 StringBuilder类不能直接转化,如果要相互转化要满足如下原则:

    • String 变为 StringBuilder:利用 StingBuilder 的构造方法或者是append()方法
    • StringBuilder 变为 String:调用 toString() 方法

    面试题

    1. String、StringBuilder、StringBuffer的区别
    • String的内容不可修改,StringBuilder和StringBuffer的内容可以修改
    • StringBuilder和StringBuffer大部分的功能相似
    • StringBuffer采用同步处理,属于线程安全操作;而StringBuilder未采用同步处理的操作,属于现成不安全操作
    1. 以下共创建了多少个String对象【前提不考虑常量池之前是否存在】
    public class Test {
        public static void main(String[] args) {
            String str1 = new String("ab");//创建多少String对象   2
            String str2 = new String("a") + new String("b");//创建多少String对象   4
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    web前端网页设计期末课程大作业:关于城市旅游的HTML网页设计 ——北京
    【luogu AT2382】A or...or B Problem(思维)
    QT object元对象
    字符串的大小(补充)
    Nginx目录分级及基本信息介绍
    史上最全的Java面试题总汇(2020年最新版)
    IT 冷知识:全球第一个“Bug”被发现
    【Vue全家桶】Vuex状态管理
    USB描述符(附加USB HID报告描述符 )
    正确主动关闭websocket,异常关闭处理
  • 原文地址:https://blog.csdn.net/fengxia110/article/details/128095023