Java之String类常用操作

在这里插入图片描述


一、String类的理解

1、类的声明

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {

final:String是不可以被继承的;
Serializable:可序列化的接口,凡是实现此接口的类的对象就可以通过网络或者本地流进行数据的传输。
Comparable:凡是实现此接口的类,其对象都可以比较大小。

2、内部声明的属性

private final char value[];

存储字符串数据的容器

final:指明此value数组一旦初始化,其地址就不可变

3、字符串常量的存储位置

字符串常量都存储在字符串常量池(StringTable)中
字符串常量池不允许存放两个相同的字符串常量
字符串常量池在不同的jdk版本中,存放的位置不同
在这里插入图片描述

4、字符串的不可变性的理解

1、当对字符串变量重新赋值时,需要重新指定一个字符串常量的位置进行赋值,不能在原来的位置修改
2、对现有的字符串进行拼接操作时,需要重新开辟空间保存新的字符串。
3、当调用字符串的replace方法替换现有的某个字符时,需要重新开辟空间保存修改以后的字符串,不能原地修改

public class StringDemo {

    public static void main(String[] args) {
        StringDemo s = new StringDemo();
        s.test2();
        s.test3();
    }

    // todo String的不可变性
    //  当对字符串变量重新赋值时,需要重新指定一个字符串常量的位置进行赋值,不能在原来的位置修改
    //  对现有的字符串进行拼接操作时,需要重新开辟空间保存新的字符串。
    //  当调用字符串的replace方法替换现有的某个字符时,需要重新开辟空间保存修改以后的字符串,不能原地修改
    public void test2() {
        String s1 = "hello";
        String s2 = "hello";

        s2 = "hi";
        s2+="world";
        System.out.println(s1);  // todo hello
        System.out.println(s1);  // todo hello
    }

    public void test3() {
        String s1 = "hello";
        String s2 = "hello";

        String s3=s2.replace('l','o');
        System.out.println(s1);   // hello
        System.out.println(s2);   // hello
        System.out.println(s3);   // heooo
    }
}



5、String实例化的两种方式

String s1 = “hello”;
String s2 = new String(“hello”);

public class StringDemo1 {
    public static void main(String[] args) {
        StringDemo1 s = new StringDemo1();
        s.test1();
    }

    public void test1(){

        String s1 = "hello";
        String s2 = "hello";

        String s3 = new String("hello");
        String s4 = new String("hello");

        System.out.println(s1==s2);  //true
        System.out.println(s1==s3);   //false
        System.out.println(s1==s4);   //false
        System.out.println(s3==s4);   //false

        System.out.println(s1.equals(s2));  //true
        System.out.println(s1.equals(s3));  //true
        System.out.println(s1.equals(s4));  //true
        System.out.println(s3.equals(s2));  //true
    }
}

6、字符串的拼接

1、常量+常量:结果仍然存储在字符串常量池;此时的常量可能是字面量,也可能是final修饰的变量。
2、常量+变量 或者 变量+常量:都会通过new的方式创建一个新的字符串,返回堆空间中此字符串对象的地址
3、调用字符串的intern():返回字面量的地址

    public void test2() {
        String s1 = "hello";
        String s2 = "world";

        String s3 = "helloworld";
        String s4 = "hello" + "world";
        String s5 = s1 + "world";    //todo 通过查看字节码文件发现调用了StringBuilder()——》new String()
        String s6 = "hello" + s2;
        String s7 = s1 + s2;
        System.out.println("------------------------------");
        System.out.println(s3 == s4);   //true
        System.out.println(s3 == s5);   //false
        System.out.println(s3 == s6);   //false
        System.out.println(s3 == s7);   //false
        System.out.println(s5 == s6);   //false
        System.out.println(s5 == s7);    //false
    }
    public void test3() {
        final String s1 = "hello";
        final String s2 = "world";

        String s3 = "helloworld";
        String s4 = "hello" + "world";
        String s5 = s1 + "world";    //todo 通过查看字节码文件发现调用了StringBuilder()——》new String()
        String s6 = "hello" + s2;
        String s7 = s1 + s2;
        System.out.println("------------------------------");
        System.out.println(s3 == s5);   //true
        System.out.println(s3 == s6);   //true
        
    }

二、String的构造器

1、构造器

public String() :初始化新创建的 String对象,以使其表示空字符序列。
public String(String original):初始化一个新创建的“String”对象,使其表示一个与参教相同的字符序列
public String(char[] value):通过当前参数中的字符数组来构造新的String。
public String(char[] valve,int offset,int count):通过字符数组的一部分来构造新的String。
public String(byte[] bytes):通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
public String(byte[] bytes,String charsetName)':通过使用指定的字符集解码当前参数中的字节数组来构造新的String。

2、String和char之间相互转换

String——》char[]:调用String的toCharArray()方法
char——》String:调用String的构造器

public class StringMethodTest {
    public static void main(String[] args) {
        StringMethodTest s = new StringMethodTest();
        s.test1();
        s.test2();
        s.test3();
    }

    public void test1() {
        String s1 = new String();
        String s2 = new String("");
        String s3 = new String(new char[]{'a', 'b', 'c'});

        System.out.println(s3);
    }

    public void test2() {
        String str = "hello";
        //todo String——》char[]:调用String的toCharArray()方法
        char[] arr = str.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        //todo char——》String:调用String的构造器
        String str1 = new String(arr);
        System.out.println(str1);       //hello
    }
}

3、String和byte之间相互转换

String——》byte[]:调用String的getBytes()方法
getBytes(String charsetName):使用指定的字符集

在utf-8字符集中,一个汉字占用3个字节,一个字母占用1个字节。
在gbk字符集中,一个汉字占用2个字节,一个字母占用1个字节。

public class StringMethodTest {
    public static void main(String[] args) {
        StringMethodTest s = new StringMethodTest();
        s.test1();
        s.test2();
        s.test3();
    }

    // String与byte[]之间的转换
    // 在utf-8字符集中,一个汉字占用3个字节,一个字母占用1个字节。
    // 在gbk字符集中,一个汉字占用2个字节,一个字母占用1个字节。
    public void test3() {
        String str = "中国";
        //todo String——》byte[]:调用String的toCharArray()方法
        byte[] arr = str.getBytes();    //使用默认的字符集
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]); // -28\-72\-83\-27\-101\-67
        }

        System.out.println();

//        String str1 = new String("abc中国");
//        // todo getBytes(String charsetName):使用指定的字符集
//        byte[] arr1 = str1.getBytes("gbk");    //使用默认的字符集
//        for(int i = 0;i<arr.length;i++){
//            System.out.println(arr[i]); // 101\101\108\108\111
//        }

        // byte[]——》String
        String str2 = new String(arr);
        System.out.println(str2);     //中国

    }

}

三、String中常用方法

1、boolean isEmpty():字符串是否为空;
2、int length():返回字符串的长度;
3、String concat(xx):字符串拼接;
4、boolean equals(Object obj):比较字符串是否相等,区分大小写;
5、boolean equalsIgnoreCase(Object obj):比较字符串是否相等,不区分大小写;
6、int compareTo(String other):比较字符串大小,区分大小写,按照Unicode编码值比较大小;
7、int compareTolgnoreCase(String other):比较字符串大小,不区分大小写;
8、String toLowerCase():将字符串中大写字母转为小写;
9、String toUpperCase():将字符串中小写字母转为大写;
10、String trim():去掉字符串前后空白符;
11、public String intern():结果在常量池中共享;
12、boolean contains(xx):是否包含xx
13、int indexOf(xx):从前往后找当前字符串中xx,即如果有返回第一次出现的下标,要是没有返回-1;
14、int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引开始向后找;
15、int lastIndexOf(xx):从后往前找当前字符串中xx,即如果有返回最后一次出现的下标,要是没有返回-1
16、int lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出现处的并且向前找;
17、String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取;
18、String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始截取,到endIndex结束;
19、char charAt(index):返回[index]位置的字符
20、char[] toCharArray(): 将此字符串转换为一个新的字符数组返回
21、static String valueOf(char[] data):返回char数组参数的字符串表示形式
22、static String valueOf(char[] data,int offset,int count): 返回char数组参数的特定子数组的字符串表示形式
23、static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的字符串。
24、static String copyValue0f(char[] data,int offset,int count):返回指定数组中指定片段的字符串。start:开始下标 count:长度
25、boolean startsWith(xx):方法用于检测字符串是否以指定的子字符串开始。
26、boolean startsWith(string prefix,int toffset):如果字符串以指定的前缀开始,则返回 true;否则返回 false。
27、boolean endsWith(xx):测试此字符串是否以指定的后结束
28、String replace(char oldchar,char newchar):返回一个新的字符串,它是通过用 newchar 替换oldchar;
29、String replace(CharSequence target,charSequence replacement):用replacement替换所有的target,两个参数都是字符串。
30、String replaceAll(String regex,String replacement):用replacement替换所有的regex匹配项,regex很明显是个正则表达式,replacement是字符串。
31、String replaceFirst(String regex,String replacement):基本和replaceAll相同,区别是只替换第一个匹配项。

评论 71
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敲代码敲到头发茂密

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

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

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

打赏作者

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

抵扣说明:

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

余额充值