• String类常用方法总结



    目录

    一.简单认识String

    二.String对象的比较

    1.equals

    内部实现原理:

    2.compareTo

    3.compareToIgnoreCase

    三.字符串查找

    示例:

    四.字符串与其他类型转化

    1.数值和字符串相互转换

    2.大小写相互转化

    3.字符串转数组

    4.格式化转化

    五.字符串替换

    全部替换

    部分替换

    六.字符串拆分

    全部拆分

    部分拆分

    特殊拆分

    七.字符串的截取

    全部截取

    部分截取


    一.简单认识String

    常见的初始化一个字符串有以下几个方式:

    1. public static void main(String[] args) {
    2. // 使用常量串构造
    3. String s1 = "hello world";
    4. System.out.println(s1);
    5. // 直接newString对象
    6. String s2 = new String("hello world");
    7. System.out.println(s1);
    8. // 使用字符数组进行构造
    9. char[] array = {'h','e','l','l','o','w','o','r','l','d'};
    10. String s3 = new String(array);
    11. System.out.println(s1);
    12. }

    String是引用类型,内部并不存储字符串本身 


    二.String对象的比较

    对于内置类型,==比较的是变量中的值;对于引用类型==比较的是引用中的地址,因此我们在对比俩个字符串的时候是不能直接用等号去判断的,在这里有以下三种方法最为常用

    1.equals

    boolean equals(Object anObject) 是按照字符大小的顺序来比较

    内部实现原理:

    1. public boolean equals(Object anObject){
    2. // 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true
    3. if (this == anObject) {
    4. return true;
    5. }
    6. // 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回false
    7. if (anObject instanceof String) {
    8. // 将anObject向下转型为String类型对象
    9. String anotherString = (String) anObject;
    10. int n = value.length;
    11. // 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回false
    12. if (n == anotherString.value.length) {
    13. char v1[] = value;
    14. char v2[] = anotherString.value;
    15. int i = 0;
    16. // 4. 按照字典序,从前往后逐个字符进行比较
    17. while (n-- != 0) {
    18. if (v1[i] != v2[i])
    19. return false;
    20. i++;
    21. }
    22. return true;
    23. }
    24. }
    25. return false;
    26. }
    1. public static void main(String[] args) {
    2. String s1 = new String("hello");
    3. String s2 = new String("hello");
    4. String s3 = new String("Hello");
    5. // s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false
    6. System.out.println(s1 == s2); // false
    7. System.out.println(s1 == s3); // false
    8. // equals比较:String对象中的逐个字符
    9. // 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
    10. // s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
    11. System.out.println(s1.equals(s2)); // true
    12. System.out.println(s1.equals(s3)); // false
    13. }

    2.compareTo

    int compareTo(String s) 与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型,compareTo返回的是字符串的差值: 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值,如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值

    1. public static void main(String[] args) {
    2. String s1 = new String("abc");
    3. String s2 = new String("ac");
    4. String s3 = new String("abc");
    5. String s4 = new String("abcdef");
    6. System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
    7. System.out.println(s1.compareTo(s3)); // 相同输出 0
    8. System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
    9. }

    3.compareToIgnoreCase

    int compareToIgnoreCase(String str) 与compareTo方式相同,但是忽略大小写比较

    1. public static void main(String[] args) {
    2. String s1 = new String("abc");
    3. String s2 = new String("ac");
    4. String s3 = new String("ABc");
    5. String s4 = new String("abcdef");
    6. System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
    7. System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
    8. System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
    9. }

    三.字符串查找

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

    方法
    功能
    char charAt (int index)
    返回 index 位置上字符,如果 index 为负数或者越界,抛出
    IndexOutOfBoundsException 异常
    int indexOf (int ch)
    返回 ch 第一次出现的位置,没有返回 -1
    int indexOf (int ch, int fromIndex)
    fromIndex 位置开始找 ch 第一次出现的位置,没有返回 -1
    int indexOf (String str)
    返回 str 第一次出现的位置,没有返回 -1
    int indexOf (String 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

    示例:

    1. public static void main(String[] args) {
    2. String s = "aaabbbcccaaabbbccc";
    3. System.out.println(s.charAt(3)); // 'b'
    4. System.out.println(s.indexOf('c')); // 6
    5. System.out.println(s.indexOf('c', 10)); // 15
    6. System.out.println(s.indexOf("bbb")); // 3
    7. System.out.println(s.indexOf("bbb", 10)); // 12
    8. System.out.println(s.lastIndexOf('c')); // 17
    9. System.out.println(s.lastIndexOf('c', 10)); // 8
    10. System.out.println(s.lastIndexOf("bbb")); // 12
    11. System.out.println(s.lastIndexOf("bbb", 10)); // 3
    12. }

    四.字符串与其他类型转化

    1.数值和字符串相互转换

    可以使用 valueOf 方法将数值转化为字符串

    1. class Student {
    2. String name;
    3. int age;
    4. public Student(String name, int age) {
    5. this.name = name;
    6. this.age = age;
    7. }
    8. }
    9. public static void main(String[] args) {
    10. // 数字转字符串
    11. String s1 = String.valueOf(1024);
    12. String s2 = String.valueOf(12.24);
    13. String s3 = String.valueOf(true);
    14. String s4 = String.valueOf(new Student("张三", 18));
    15. System.out.println(s1);
    16. System.out.println(s2);
    17. System.out.println(s3);
    18. System.out.println(s4);
    19. }

    我们想将整形的双精度浮点型的数值转化为字符串就使用它的包装类 Integer、Double 以及相对应的方法

    1. public static void main(String[] args) {
    2. // 字符串转数字
    3. int data1 = Integer.parseInt("1024");
    4. double data2 = Double.parseDouble("10.24");
    5. System.out.println(data1);
    6. System.out.println(data2);
    7. }

    2.大小写相互转化

    • 小写转大写: toUpperCase 
    • 大写转小写: toLowerCase 
    1. public static void main(String[] args) {
    2. String s1 = "hello";
    3. String s2 = "HELLO";
    4. // 小写转大写
    5. System.out.println(s1.toUpperCase());
    6. // 大写转小写
    7. System.out.println(s2.toLowerCase());
    8. }

    3.字符串转数组

    可以使用 toCharArray 方法来将字符串转化为字符数组

    1. public static void main(String[] args) {
    2. String s = "hello";
    3. // 字符串转数组
    4. char[] ch = s.toCharArray();
    5. for (int i = 0; i < ch.length; i++) {
    6. System.out.print(ch[i]);
    7. }
    8. System.out.println();
    9. // 数组转字符串
    10. String s2 = new String(ch);
    11. System.out.println(s2);
    12. }

    4.格式化转化

    使用 format 方法可以帮助我们将带有格式的字段转化为字符串

    1. public static void main(String[] args) {
    2. String s = String.format("%d-%d-%d", 2023, 11, 23);
    3. System.out.println(s);
    4. }

    五.字符串替换

    Java允许我们使用一段字符串替换掉原有字符串中的部分字段,我们可以有以下俩种方式

    全部替换

    我们可以使用 replaceAll 方法,该方法允许我们将整个字符串中选中的部分全部替换

    1. public static void main(String[] args) {
    2. String str1 = "helloworld" ;
    3. System.out.println(str1.replaceAll("l", "_"));
    4. }

    部分替换

    我们可以使用 replaceFirst 方法来将字符串中首个出现的选中的字符进行替换

    1. public static void main(String[] args) {
    2. String str2 = "helloworld" ;
    3. System.out.println(str2.replaceFirst("l", "_"));
    4. }

    注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串


    六.字符串拆分

    Java中也允许我们全部拆分字符串或者部分拆分字符串

    全部拆分

    我们可以使用 split 方法来做到全部拆分

    1. public static void main(String[] args) {
    2. String str = "hello world hello friend" ;
    3. String[] result = str.split(" ") ; // 按照空格拆分
    4. for(String s: result) {
    5. System.out.println(s);
    6. }
    7. }

    部分拆分

    我们可以使用 split 方法来做到部分拆分,只需要再额外传入一个参数就可以,我们在第二个参数传入多少就会被分成多少分,默认从左往右分

    1. public static void main(String[] args) {
    2. String str = "hello world hello friend" ;
    3. String[] result = str.split(" ",2) ;
    4. for(String s: result) {
    5. System.out.println(s);
    6. }
    7. }

    特殊拆分

    拆分是特别常用的操作,另外有些特殊字符作为分割符可能无法正确切分,需要加上转义,比如我们这里的 “ . ” 在正常情况下是无法识别的,需要加转义符

    1. public static void main(String[] args) {
    2. String str = "192.168.1.1" ;
    3. String[] result = str.split("\\.") ;
    4. for(String s: result) {
    5. System.out.println(s);
    6. }
    7. }

    注意事项: 

    • 字符" | "," * "," + "都得加上转义字符,前面加上 " \\ "
    • 而如果是 " \ " ,那么就得写成 " \\\\ " 
    • 如果一个字符串中有多个分隔符,可以用 " | " 作为连字符

    示例:

    1. public static void main(String[] args) {
    2. String str = "name=zhangsan&age=18" ;
    3. String[] result = str.split("&") ;
    4. for (int i = 0; i < result.length; i++) {
    5. String[] temp = result[i].split("=") ;
    6. System.out.println(temp[0]+" = "+temp[1]);
    7. }
    8. }

    七.字符串的截取

    Java允许我们从一个完整的字符串之中截取出部分

    全部截取

    我们可以使用 substring 方法从我们指定的索引截取到结尾

    1. public static void main(String[] args) {
    2. String str = "helloworld";
    3. System.out.println(str.substring(5));
    4. }

    部分截取

    我们可以使用 substring 方法加上一个参数后,只截取一部分内容

    1. public static void main(String[] args) {
    2. String str = "helloworld";
    3. System.out.println(str.substring(0, 5));
    4. }

    其中需要注意:索引从0开始,并且是属于前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标




      本次的分享就到此为止了,希望我的分享能给您带来帮助,也欢迎大家三连支持,你们的点赞就是博主更新最大的动力!如有不同意见,欢迎评论区积极讨论交流,让我们一起学习进步!有相关问题也可以私信博主,评论区和私信都会认真查看的,我们下次再见! 

  • 相关阅读:
    把Eclipse创建的Web项目(非Maven)导入Idea
    Maven介绍及仓库配置
    Temporal对比Cadence
    软件考试:计算机组成原理:程序的运行过程
    上周热点回顾(2.21-2.27)
    数仓模型设计方法论
    Canny算子详解及例程
    C++ Reference: Standard C++ Library reference: Containers: array: array: end
    HTML <th> 标签
    React-组件-props 校验 类型 是否必填 默认值
  • 原文地址:https://blog.csdn.net/m0_69519887/article/details/134529066