String类的详解

一、字符串的构造

       String类的构造方法非常多,常用的就以下三种:使用常量串构造、直接newString对象构造

使用字符串数组进行构造

public static void method1(){//验证String对象常见的构造方法
        String s1 = "hello";
        System.out.println(s1);

        String s2 = new String("hello");
        System.out.println(s2);

        char[] ch = {'h','e','l','l','o'};
        String s3 = new String(ch);
        System.out.println(s3);
    }

注:1、String是引用类型,内部并不储存字符串本身,而储存的是String对象的地址,在JDK1.8中,字符串实际保存在char类型的数组中

       2、String类不能被继承,String类实现了Comparable接口,因为String类的对象可以直接比较,或者可以直接排序

        3、在Java中被“”引起来的也是String类型对象

二、String对象的比较

      Java中总共提供了4种方式

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

          注意:对于内置对象,==比较的是变量中的值,对于引用类型,==比较的是引用中的地址

public static void method2(){//String为引用类型
        String s1 = "hello";
        String s2 = new String("hello");
        String s3 = s1;

        System.out.println(s1 == s2);
        System.out.println(s1 == s3);
    }

     2、 比较字符串中的内容用equals()

           boolean equals(Object anObject)方法:按照字典序(字符大小的顺序)比较

           String类重写了父类Object中的equals方法

//比较字符串中的内容用equals()
    public static void method4(){
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = new String("hello");
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
    }

    3、比较字符串中的内容也可以用compareTo()

            int compareTo(String s):按照字典序进行比较,返回int类型,具体比较方式为:

           1)先按照字典序大小进行比较,如果出现不等的字符,直接返回这两个字符的大小差值

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

//比较字符串中的内容也可以用compareTo()
    public static void method5(){
        String s1 = new String("abc");
        String s2 = new String("a");
        String s3 = new String("abcdef");
        String s4 = new String("abd");
        String s5 = new String("abc");
        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
        System.out.println(s1.compareTo(s4));
        System.out.println(s1.compareTo(s5));
    }

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

三、字符串查找

        几种常用的方法

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 method6() {
        String s1 = String.valueOf(123);
        String s2 = String.valueOf(12.3);
        String s3 = String.valueOf(false);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        //数值类型的字符串转化为数值
        int data = Integer.parseInt("1234");
        double d = Double.parseDouble("12.34");
        System.out.println(data);
        System.out.println(d);
    }

      2、字符串转数组

//字符串和数字之间的转换
        char[] c = s3.toCharArray();
        String s4 = new String(c);

      3、大小写转换

   public static void method6() {
        //大小写转化
        String s = "ADcgD";
        s = s.toLowerCase();
        System.out.println(s);
        s = s.toUpperCase();
        System.out.println(s);
    }

     4、 格式化

public static void main(String[] args) {
        //格式化
        String date = String.format("%d-%d-%d",2022,9,20);
        System.out.println(date);
    }

注:

  1、short和byte这两种类型的数据可以使用int类型来接收,即short和byte可以隐式转换为int 

  2、将自定义类型的对象转换为String类型的字符串,类中需要重写Object类中的toString的方法,如果没有重写,将来转换完之后的结果:类全路径名+@+hashCode的十六进制的结果

五、字符串替换

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

String replaceAll(String regex,String replacement)
String replaceFirst(String regex,String replacement)
//测试字符串的替换
    public static void method1(){
        String s1 = "aaaaaaa";
        System.out.println(s1.replaceAll("a","b"));
        System.out.println(s1.replaceFirst("a","b"));
    }

注意:replaceAll方法并不是在s的基础上替换的,而是将替换的结果作为一个新的字符串返回了,替换之后始终不变,因为String类在设计的时候,就严格限定了String类的对象是不可改变的,即字符串是不可变对象

六、字符串拆分

     可以将一个完整的字符串按照指定的分隔符划分为若干个字符串

String[] split(String regex)
//将字符串全部拆分
String[] split(String regex,int limit)
//将字符串以指定的格式,拆分为limit组
//字符串的分割
    public static void method2(){
        String s = "apple bear pink glass";
        String[] ans = s.split(" ");
        for(int i = 0;i < ans.length;i++){
            System.out.println(ans[i]);
        }
        System.out.println("=====================");
        String s1 = "apple,bear,pink,glass";
        String[] ans1 = s1.split(",",2);
        for(int i = 0;i < ans1.length;i++){
            System.out.println(ans1[i]);
        }
    }

注意: 

     1、字符“|”,“*”,“+”都得加上转义字符,前面加上“//”

     2、如果是“\”,那么就得写成“\\\\”

     3、如果一个字符串中有多个分隔符,可以用“|”作为连字符

七、字符串截取

     从一个完整的字符串之中截取部分内容,可用方法如下

String substring(int beginIndex)
String substring(int beginIndex,int endIndex)
//字符串的截取
    public static void method3(){
        String s = "hello world!!!";
        String ans = s.substring(s.indexOf("world"));
        System.out.println(ans);

        int start = s.indexOf("world");
        ans = s.substring(start,start + "world".length());
        System.out.println(ans);
    }

注意:

   1、索引从0开始

    2、注意前闭后开,substring (0,5)包含0号下标的字符,不包含5号下标的字符

八、其他操作方法

String trim()
//去掉字符串中的左右空格,保留中间空格
 //其他操作
    public static void method4(){
        String s = "   ahdurS12 &$ 哈哈#&fe  ";
        String s1 = s.trim();
        System.out.println(s1);
    }

九、String、StringBuffer、StringBuilder的区别

   1、String的内容不可修改,StringBuffer、StringBuilder的内容可以修改

    2、StringBuffer、StringBuilder的大部分功能是相似的

    3、StringBuffer采用同步处理,属于线程安全操作,而StringBuilder未采用同步处理,属于线程不安全操作

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薰衣草2333

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值