• String类


    一:字符串构造

    String类常用的构造方法:
    1:使用字符串构造

     String s1="hello world";
            System.out.println(s1);//hello world
    
    
    
    • 1
    • 2
    • 3
    • 4

    2: 直接newString对象

     String s2=new String("hello World");
            System.out.println(s2);//hello world
    
    • 1
    • 2

    3: 使用字符数组进行构造

     char[] ch={'h','e','l','l','o'};
            String s3=new String(ch);
            System.out.println(s3);//hello
    
    • 1
    • 2
    • 3

    #二: String对象的比较

    1:==比较是否引用同一个对象

            String s1=new String("hello");
            String s2=new String("hello");
            System.out.println(s1==s2);//false
            //s1,s2是创建的不同对象,指向了不同的空间,
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    但如果直接给字符串赋值,则引用的是同一个对象,但这里涉及到常量池的知识(可以简单认为这个常量池同样的东西只存放一份)

    String s3="hello";
            String s4="hello";
            //s3,s4所指向的对象的内容相同 ,"hello"放在了常                                           量池中,所以s3,s4指向了同一个对象
            
            System.out.println(s3==s4);//true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2:equals(),按照字典序比较是否相同

    因为String类重写了父类Object中的equals()方法,所以可以使用equals()方法比较两个字符串的内容是否相同。

      String str1=new String("hello");
            String str2=new String("hello");
            String str3=new String("he");
            System.out.println(str1.equals(str2));//true
            System.out.println(str1.equals(str3));//false
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3:equalsIgnoreCase()忽略大小写比较两个字符串是否相等。

            String s1=new String("hello");
            String s2=new String("Hello");
            String s3=new String("HELLO"); 
            System.out.println(s1.equalsIgnoreCase(s2));//true
            //忽略大小写比较是否 相等
            System.out.println(s1.equalsIgnoreCase(s3));//true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4:compareTo()比较两个字符串的大小

    因为String类重写了父类Object中的compareTo()方法,所以可以使用compareTo()方法比较两个字符串的内容是否相同。
    比较情况:
    1:
    在比较的时候先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符大小的差值;

       String s1="hello";
            String s2="i";
            System.out.println(s1.compareTo(s2));//'h'-'i'=-1
    
    • 1
    • 2
    • 3

    2:如果前k个字符相等(k为两个字符串的最小值),那么返回两个字符串长度的差值。

       String s3="hello";
            String s4="helloworld";
            System.out.println(s3.compareTo(s4));//字符串s3的长度减去字符串s4的长度
    
    • 1
    • 2
    • 3

    5:compareToIgnoreCase()忽略大小写大小比较:

            String s1=new String("hello");
            String s2=new String("Hello");
            String s3=new String("HELLO");
            System.out.println(s2.compareToIgnoreCase(s3));//0
            System.out.println(s1.compareToIgnoreCase(s3));//0
    
    • 1
    • 2
    • 3
    • 4
    • 5

    三:String类常用的方法

    1:字符串查找:

     String s1="hellohehlee";
            System.out.println(s1.charAt(1));//查找1下标的字符,并返回该字符
            System.out.println(s1.indexOf('l'));//从前往后查找字符'l',并返回第一次'l'的下标,没找到返回-1
            System.out.println(s1.indexOf("he"));//从前往后查找字符串"he",并返回第一次"he"的下标,没找到返回-1
            System.out.println(s1.indexOf('l',4));//从下标4开始查找字符'l',并返回第一次找到的下标,没找到返回-1
            System.out.println(s1.indexOf("he",6));//从下标6开始查找字符串"he",并返回第一次找到的下标,没找到返回-1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    补充:

       System.out.println(s1.lastIndexOf('e'));//从后往前查找字符'e',并返回第一次'e'的下标,没找到返回-1
            System.out.println(s1.lastIndexOf('e',8));//从下标8开始从后往前找字符'e',并返回第一次'e'的下标,没找到返回-1
            System.out.println(s1.lastIndexOf("he"));//从后往前查找字符串"he",并返回第一次"he"的下标,没找到返回-1
            System.out.println(s1.lastIndexOf("he",2));//从下标2开始从后往前找字符串"he",并返回第一次"he"的下标,没找到返回-1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    2:转化

    1:字符串和整数间的转化

     String s1=String.valueOf(123);//把整数123转化成字符串s1
            System.out.println(s1);
            int ret=Integer.valueOf("123");//把字符串"123"转化成整数123
            System.out.println(ret+10);
    
    • 1
    • 2
    • 3
    • 4

    2:字符串和数组间的转化

            String s="hello";
            char[] ch=s.toCharArray();//把字符串转变成数组
            for (char c: ch) {
                System.out.println(c);
            }
            char[] s1={'a','b','c','d'};
            String s2=new String(s1);//把数组转变成字符串
            System.out.println(s2);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3:大小写转化

    String s1="hello";
            String s2=s1.toUpperCase();//把s1字符串全部转变成大写的
            System.out.println(s2);
            String s3="Hello World";
            String s4=s3.toLowerCase();//把s3字符串全部转变成小写的
            System.out.println(s4);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述4:格式化

            String s=String.format("%d->%d->%d",2023,11,18);
            System.out.println(s);//2023->11->18
            System.out.println(String.format("%d-%d-%d",2023,11,18));//2023-11-18
    
    • 1
    • 2
    • 3

    3:替换

       String name="I am wang qi";
            System.out.println(name.replace("wang qi","zhang san"));
            String s="hello world hello hello";
            System.out.println(s.replace("hello","hi"));//全部替换
            System.out.println(s.replaceAll("hello","hi"));//全部替换
            System.out.println(s.replaceFirst("hello","hi"));//替换第一个
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4:拆分

            String s1="hello world hi wuqu";
            String[] s2= s1.split(" ");//按空格拆分
            for (String s: s2) {
                System.out.println(s);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
       String s1="hello world hi wuqu";
            String[] s2= s1.split(" ",2);//按空格拆分,并拆成两部分
            for (String s: s2) {
                System.out.println(s);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
     String s="hello world hi hi";
            System.out.println(s.split(" ",2));
            //按空格拆分成两部分
            System.out.println(s.split(" ",12));
            //按空格拆分12部分,但最多截取4成部分
    
    • 1
    • 2
    • 3
    • 4
    • 5

    String s=“hello world hi hi”;
    String[] s1= s.split(" “,2);
    //按空格拆分成两部分
    for (String ss: s1) {
    System.out.println(ss);
    }
    System.out.println(”---------------------“);
    String[] s2=s.split(” ",12);
    //按空格拆分12部分,但最多截取4成部分
    for (String ss: s2) {
    System.out.println(ss);
    }

    多次拆分

            String name="name=zhangsan&age=20";
            String[] ch=name.split("&");//按&拆分
            for (String s: ch) {
                String[] ss=s.split("=");
                for (String s1: ss) {
                    System.out.println(s1);
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5:截取

            String s="hello world";
            System.out.println(s.substring(1));//从1下标开始截取
            System.out.println(s.substring(1,3));//左闭右开截取
    
    • 1
    • 2
    • 3
  • 相关阅读:
    论文阅读【3】Efficient Estimation of Word Representations in Vector Space
    10.20作业
    Ra-08系列开发板入门教程,标准LoRaWAN协议对接国外 TTN LoRaWAN 开源服务器。
    【Linux】缓冲区+磁盘+动静态库
    一文教你普罗米修斯Prometheus的基础应用
    使用numba cuda 加速Python运算
    Taurus.MVC 微服务框架 入门开发教程:项目部署:7、微服务节点的监控与告警。
    磁盘阵列(RAID)级别的简单介绍
    【Windows Server 2019】路由服务的配置和管理
    华为机试:最小传输时延
  • 原文地址:https://blog.csdn.net/2302_77978695/article/details/134478702