• 七千字详解javaString类


    在这里插入图片描述

    一、String类

    在C语言中要表示字符串只能使用字符数组或者字符指针,在java中专门提供了String类.

    1. String初始化

    1.常量串构造

    public static void main(String[] args) {
            String str1 = "dachang";
        }
    
    • 1
    • 2
    • 3

    2.new String对象

     public static void main(String[] args) {
            String str2 = new String("dachang");
        }
    
    • 1
    • 2
    • 3

    3.使用字符数组构造

    public static void main(String[] args) {
            char[] ch = {'d','a','c','h','a','n','g'};
            String str3 = new String(ch);
        }
    
    • 1
    • 2
    • 3
    • 4

    2. String具体存储形式

    String是引用类型,内部不存储字符串本身,而是存储的一个地址.我们打开String的源码看一下
    在这里插入图片描述
    字符串由两部分组成char[ ]和hash两部分组成,String实际保存在char数组中.

    public static void main(String[] args) {
            String s1 = new String("dachang");
            String s2 = new String("woyao");
            String s3 = s1;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    我们具体看一下在堆栈上是如何存储的.
    在这里插入图片描述

    二、String中的比较

    1. ==比较是否引用同一个对象
    public static void main(String[] args) {
            String s1 = new String("dachang");
            String s2 = new String("dachang");
            System.out.println(s1 == s2);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    2.equals方法.按照具体字符串内容比较.
    在这里插入图片描述
    我们可以发现Object中的equals方法是按照==方式,比较的是引用地址.
    在这里插入图片描述
    String类中重写了Object中的equals方法,按照字符串中的内容比较.

     public static void main(String[] args) {
            String s1 = new String("dachang");
            String s2 = new String("dachang");
            System.out.println(s1.equals(s2));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    3. compareTo方法.
    equals只能比较两个字符串是否相等,返回一个boolean类型.但如果你要知道谁大谁小这时候equals就不能满足了.

    1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
    2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
      在这里插入图片描述
      我们发现String类是实现了Comparable接口的.
    public static void main(String[] args) {
            String s1 = new String("abc");
            String s2 = new String("abb");
            System.out.println(s1.compareTo(s2));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    忽略大小写的比较:
    compareToIgnoreCase方法与comparaTo相同但忽略大小写.

    public static void main(String[] args) {
            String s1 = new String("abc");
            String s2 = new String("ABC");
            System.out.println(s1.compareToIgnoreCase(s2));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    三、字符串查找

    1. charAt(int index)

    public static void main(String[] args) {
            String str = new String("hello");
            System.out.println(str.charAt(2));
        }
    
    • 1
    • 2
    • 3
    • 4

    charAt()方法是返回index下标的字符
    在这里插入图片描述
    2.indexOf(int ch)

    public static void main(String[] args) {
            String str = new String("hello");
            System.out.println(str.indexOf('l'));
        }
    
    • 1
    • 2
    • 3
    • 4

    indexOf()方法是返回ch字符第一次出现的位置,没有返回-1
    在这里插入图片描述
    3.indexOf(String str)

    public static void main(String[] args) {
            String str = new String("hello");
            System.out.println(str.indexOf("el"));
        }
    
    • 1
    • 2
    • 3
    • 4

    indexOf()方法返回str字符串第一次出现的位置,没有返回-1
    在这里插入图片描述
    4.lastIndex(int ch)

    public static void main(String[] args) {
            String str = new String("hello");
            System.out.println(str.lastIndexOf('l'));
        }
    
    • 1
    • 2
    • 3
    • 4

    lastindexOf()方法是从后往前找返回ch字符第一次出现的位置,没有返回-1
    在这里插入图片描述
    5.lastIndex(String str)

    public static void main(String[] args) {
            String str = new String("hello");
            System.out.println(str.lastIndexOf("lo"));
        }
    
    • 1
    • 2
    • 3
    • 4

    lastindexOf()方法是从后往前找返回str字符串第一次出现的位置,没有返回-1
    在这里插入图片描述

    四、字符串转化

    1. 大小写转化

    toUpperCase()小写转大写,其他字符不变

    public static void main(String[] args) {
            String str = new String("hello");
            System.out.println(str.toUpperCase());
        }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    toLowerCase()小写转大写,其他字符不变

    public static void main(String[] args) {
            String str = new String("HELLO");
            System.out.println(str.toLowerCase());
        }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    2. 数值字符串相互转化

    数值转字符串

    class Person{
        public String name;
        public int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
        public static void main(String[] args) {
            String s1 = String.valueOf(100);
            String s2 = String.valueOf(10.5);
            String s3 = String.valueOf(true);
            String s4 = String.valueOf(new Person("zhangsan",21));
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(s4);
        }
    
    • 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

    实例类型转字符时需要重写toString()方法,不然输出的时路径@哈希地址
    在这里插入图片描述
    字符串转数值

    public static void main(String[] args) {
            int a = Integer.parseInt("110");
            double b = Double.parseDouble("99.5");
            System.out.println(a);
            System.out.println(b);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    这里使用到了Integer包装类型.
    在这里插入图片描述

    3. 字符串转数组

    public static void main(String[] args) {
            String str = new String("abcd");
            char[] ch = str.toCharArray();
            for (int i = 0; i < ch.length; i++) {
                System.out.print(ch[i]+" ");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    4. 格式化字符串

    public static void main(String[] args) {
            String str = String.format("%d-%d-%d",2022,8,31);
            System.out.println(str);
        }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    五、字符串截取与替换

    1. trim()

    去掉字符串左右所有的空格会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等

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

    在这里插入图片描述

    2. subString()

    传入一个参数,是截取此位置到结尾.

    public static void main(String[] args) {
            String str = "hello world!";
            System.out.println(str.substring(6));
        }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    传入两个参数,是截取指定范围内容

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

    在这里插入图片描述

    3.replace()

    使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:

    public static void main(String[] args) {
            String str = new String("2022.8.31");
            System.out.println(str.replace('.','-'));
        }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    如果只想替换第一个内容使用replaceFirst()即可

    六、字符串拆分

    方法功能
    String[] split(String regex)字符串全部拆分
    String[] split(String regex, int limit)将字符串以指定的格式,拆分为limit组
    public static void main(String[] args) {
            String str = "wo yao jin da chang";
            String[] arr = str.split(" ");
            for (String s: arr) {
                System.out.println(s);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    public static void main(String[] args) {
            String str = "wo yao jin da chang";
            String[] arr = str.split(" ",2);
            for (String s: arr) {
                System.out.println(s);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    特殊情况的拆分:
    1.如果一个字符串中有多个分隔符,可以用"|"作为连字符.

    public static void main(String[] args) {
            String str = "wo&jin=da=chang";
            String[] arr = str.split("=|&");
            for (String s:arr) {
                System.out.println(s);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    2. 字符"|“,”*“,”+"都得加上转义字符,前面加上 “\”

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

    在这里插入图片描述
    3. 如果是 “” ,那么就得写成 “\\” .

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

    在这里插入图片描述

  • 相关阅读:
    书生·浦语大模型第二期实战营第六节-Lagent & AgentLego 智能体应用搭建 笔记和作业
    Halcon 2D-Transformation 相关算子(一)
    sstream及按格式字符分割字符串
    机器学习 | 过拟合&欠拟合
    淘宝/天猫API接口,item_sku - 淘宝商品SKU详细信息查询,淘宝/天猫获取sku详细信息 API 返回值说明
    101页4万字数字孪生能源互联网智慧能源物联网大数据建设方案
    unity shader用渲染纹理实现镜子效果
    MybatisPlus中queryWrapper的or的使用
    Unity获取脚本的CustomEditor(自定义编辑)数据
    趣味算法-读书笔记(三)
  • 原文地址:https://blog.csdn.net/buhuisuanfa/article/details/126610226