String及相关类

在这里插入图片描述

String类

  1. 概念:String字符串就是由多个字符组成的一串字符序列

  2. 定义格式
    方式一:采用new方式
    例如:String str = new String(“abc”);
    方式二:采用字面量直接赋值的方式
    例如:String str = “abc”;

  3. 特点:字符串是一个常量,一旦被创建,不可改变,指的是字符串的内容不可改变,因为它是在方法区的字符串池分配地址,但可以改变指向
    例如:String s=“hello”;
    s=“abc”;
    System.out.println(s);
    输出的就是abc,并不是说hello被改变了,只是指向变成了abc

String类的常见方法

  1. 构造方法
    public String(byte[] bytes):把字节数组转换成字符串
    public String(byte[] bytes,int index,int length):把字节数组从下标index开始到length个元素转换成字符串
    public String(char[] chars):把字符数组转换成字符串
    public String(char[] chars,int index,int count):把字符数组一部分转换成字符串
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //定义一个字节数组
        byte[] bytes = {97,98,99,100};
        //把字节数组转换成字符串
        String s = new String(bytes);
        System.out.println(s.toString());
        //把字节数组从指定下标开始转换一定长度的元素为字符串
        String s1 = new String(bytes,1,3);
        System.out.println(s1);
        System.out.println("----------------");
        //定义一个字符数组
        char[] chars = {65,66,67,68,69};
        //把字符数组转换成字符串
        String str = new String(chars);
        System.out.println(str);
        //把字符数组从指定下标开始转换一定长度的元素为字符串
        String str2 = new String(chars,2,3);
        System.out.println(str2);
    }
}

运行结果:
在这里插入图片描述
这里在输出时自动将数字根据ASCII码表转换成单个字符,但不影响方法的使用
String类重写了toString方法,打印输出的是字符串内容

  1. 判断功能
    public boolean equals(Object obj):比较两个字符串的内容是否相等,区分大小写
    public boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相等,不区分大小写
    public boolean contains(String str):判断字符串中是否包含传递进来的字符串
    public boolean startsWith(String str):判断字符串是否以传递进来的字符串开头
    public boolean endsWith(String str):判断字符串是否以传递进来的字符串结尾
    public boolean isEmpty():判断字符串是否为空串
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //比较两个字符串的内容是否一样,区分大小写
        boolean b = "aaa".equals("aaa");
        System.out.println(b);//true
        boolean b1 = "aaa".equals("AAA");
        System.out.println(b1);//false
        //比较两个字符串的内容是否一样,不区分大小写
        boolean b2 = "qqq".equalsIgnoreCase("qqq");
        System.out.println(b2);//true
        boolean b3 = "qqq".equalsIgnoreCase("QQQ");
        System.out.println(b3);//true
        //判断字符串中是否包含传递进来的字符串
        boolean b4 = "zxcv".contains("xc");
        System.out.println(b4);//true
        //判断字符串中是否以传进来的字符开头
        boolean b5 = "asdf".startsWith("as");
        System.out.println(b5);//true
        //判断字符串中是否以传进来的字符结尾
        boolean b6 = "qwer".endsWith("r");
        System.out.println(b6);//true
        //判断字符是否是空串
        boolean b7 = "rty".isEmpty();
        System.out.println(b7);//false
    }
}

对比下运行结果:
在这里插入图片描述
String类重写了equals方法,比较的是两个字符串的内容是否一样

  1. 获取功能
    public int length():获取字符串长度
    public char charAt(int index):获取指定索引位置的字符
    public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
    public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
    public int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
    public int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引
    public String substring(int start):从指定位置开始截取字符串,一直到末尾
    public String substring(int start,int end):从指定位置开始到指定位置结束截取字符串
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //定义一个字符串
        String str = "helloworld";
        //获取字符串长度
        int length = str.length();
        System.out.println(length);
        //获取指定索引位置的字符
        char ch = str.charAt(2);
        System.out.println(ch);//l
        //获取指定字符第一次出现的索引值
        int index = str.indexOf('w');
        System.out.println(index);//5
        //获取指定字符从指定位置后第一次出现的索引值
        int index2 = str.indexOf('l',4);
        System.out.println(index2);//8
        //获取指定字符串第一次出现的索引值
        //这里返回的索引值是指定字符串的第一个字符的索引值
        int index3 = str.indexOf("owo");
        System.out.println(index3);//4
        //获取指定字符串从指定位置后第一次出现的索引值
        int index4 = str.indexOf("l",4);
        System.out.println(index4);//8
        //顺带一提,之前的indexOf方法都是从前往后获取
        //另外一种就是从后往前获取,就是lastindexOf方法,用法跟indexOf是一样的
        //以获取指定字符第一次出现的索引值为例
        //用lastindexOf方法就是获取从后到前第一次出现指定字符的索引值
        int index5 = str.lastIndexOf('o');
        System.out.println(index5);//6
        //从指定位置开始截取字符串,一直到末尾
        String s = str.substring(2);
        System.out.println(s);//lloworld
        //从指定位置开始到指定位置结束截取字符串
        //这里注意,截取的部分含头不含尾,也就是说实际截取的是索引值2-5的字符
        String s1 = str.substring(2,6);
        System.out.println(s1);//llow
    }
}

运行结果:
在这里插入图片描述
一些获取的使用方法和注意事项在代码上都有注释

  1. 转换功能
    public byte[] getBytes():把字符串转换成字节数组
    public char[] tocharArray():把字符串转换成字符数组
    public static String valueOf(char[] ch):把字符数组转换成字符串
    public static String valueOf(int i):把int类型的数据转换成字符串
    public String toLowerCase():把字符串转换成小写
    public String toUpperCase():把字符串转成大写
    public String concat(String str):拼接字符串
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //定义一个字符串
        String str = "helloworld";
        //字符串转成字节数组
        byte[] bytes = str.getBytes();
        //遍历输出数组元素
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i]+" ");
        }
        System.out.println();
        //字符串转成字符数组
        char[] chars = str.toCharArray();
        //遍历输入字符数组
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]+" ");
        }
        System.out.println();
        //字符数组转成字符串
        //定义一个字符数组
        char[] ch = {'a','b','c'};
        String s = String.valueOf(ch);
        System.out.println(s);
        //int类型的数据转成字符串
        //valueOf方法可以将任意类型的数据转换成字符串
        int i = 100;
        String s1 = String.valueOf(i);
        System.out.println(s1);
        //把字符串转成大写
        String s2 = str.toUpperCase();
        System.out.println(s2);
        //把字符串转成小写
        String s3 = s2.toLowerCase();
        System.out.println(s3);
        //拼接字符串
        String s4 = str.concat("abc");
        System.out.println(s4);
    }
}

运行结果:
在这里插入图片描述
5. 其他功能
public String replace(char old,char new):用new字符替换old字符
public String replace(String old,String new):用new字符串替换old字符串
public String trim():去除两端空格
public int compareTo(String str):对照ASCII码表,从第一个字母进行减法运算,返回运算结果,区分大小写
public int compaerToIgnoreCase(String str):对照ASCII码表,从第一个字母进行减法运算,返回运算结果,不区分大小写
演示

package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //定义一个字符串
        String str = "  helloworld  ";
        //替换指定字符
        String s = str.replace('l','q');
        System.out.println(s);
        //替换指定字符串
        String s1 = str.replace("owo","zzz");
        System.out.println(s1);
        //去除两端空格
        String s2 = str.trim();
        System.out.println(s2);
        //按字典顺序比较两个字符串
        int result = str.compareTo("  helloworld  ");
        //减法运算后结果是0,表明两个字符串内容一样
        System.out.println(result);
        //按字典顺序比较两个字符串,忽略大小写
        String s3 = str.toUpperCase();
        int result2 = s3.compareToIgnoreCase("  helloworld  ");
        System.out.println(result2);
    }
}

运行结果:
在这里插入图片描述
这里只列举了String类的一些常用方法,还有很多其他的方法,在需要使用的时候可以根据API文档进行查看并学习,方法有很多,想要灵活运用,在记住这些方法的前提下就是多多使用,用的多了,自然就熟悉了

StringBuffer类

  1. 概述:线程安全的可变字符序列
  2. 特点:拿String相比,String类字符串一旦定义好,它的内容和长度就不可改变,而StringBuffer相当于一个字符容器,它的长度和内容都是可变的,当我们创建一个StringBuffer对象时,初始容量为16个字符,在不断拼接字符的时候,若字符容量大于16个字符,StringBuffer会自动扩大容量
  3. 格式:StringBuffer stringBuffer = new StringBuffer();

StringBuffer类的常用方法

  1. 构造方法
    public StringBuffer():空参构造方法
    public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
    public StringBuffer(int capacity):指定容量的字符串缓冲区对象
    public int capacity():返回当前容量
    public int length():返回长度
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //调用空参构造创建对象
        StringBuffer stringBuffer = new StringBuffer();
        //创建对象并指定容量大小
        StringBuffer stringBuffer1 = new StringBuffer(1000);
        //创建对象并指定字符串
        StringBuffer stringBuffer2 = new StringBuffer("abc");
        //获取容器容量
        //默认容量大小16个字符
        int capacity = stringBuffer.capacity();
        System.out.println(capacity);
        //获取容器里字符串长度
        int length = stringBuffer2.length();
        System.out.println(length);
    }
}

运行结果:
在这里插入图片描述

  1. 添加功能
    public StringBuffer append(String str):把任意类型数据添加到字符串缓冲区里
    public StringBuffer insert(int offset,String str):在指定位置把任意类型数据插入到字符串缓冲区里
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //创建对象并指定字符串
        StringBuffer stringBuffer = new StringBuffer("abc");
        //向字符串缓冲区中添加内容
        stringBuffer.append(100);
        System.out.println(stringBuffer);
        //在指定位置向字符串缓冲区中插入内容
        stringBuffer.insert(3,"ABC");
        System.out.println(stringBuffer);
    }
}

运行结果:
在这里插入图片描述
这里注意,StringBuffer方法调用完之后都是返回字符串本身,所以不需要在重新定义一个
另外,在调用插入方法时,是在所传入的索引值之前插入内容

  1. 删除功能
    public StringBuffer deleteCharAt(int index):删除指定位置的字符
    public StringBuffer delete(int start,int end):删除从指定位置开始到指定位置结束的内容
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //创建对象并指定字符串
        StringBuffer stringBuffer = new StringBuffer("abcdefghj");
        //删除指定位置字符
        stringBuffer.deleteCharAt(2);
        System.out.println(stringBuffer);
        //删除指定位置的部分字符串
        //删除范围含头不含尾
        stringBuffer.delete(2,5);
        System.out.println(stringBuffer);
    }
}

运行结果:
在这里插入图片描述

  1. 替换和反转功能
    public StringBuffer replace(int start,int end,String str):把索引值是start到end-1的字符串用str字符串代替
    public StringBuffer reverse():反转字符串
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //创建对象并指定字符串
        StringBuffer stringBuffer = new StringBuffer("abcdefghj");
        //替换部分字符串(含头不含尾)
        stringBuffer.replace(2,5,"aaa");
        System.out.println(stringBuffer);
        //反转字符串
        stringBuffer.reverse();
        System.out.println(stringBuffer);
    }
}

运行结果:
在这里插入图片描述

  1. 截取功能
    public String substring(int start):从指定位置截取到末尾
    public String substring(int start,int end):截取start到end字符串
    演示
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //创建对象并指定字符串
        StringBuffer stringBuffer = new StringBuffer("abcdefghj");
        //从指定位置截取字符串到末尾(包括指定位置字符)
        String str = stringBuffer.substring(3);
        System.out.println(str);
        //截取一部分字符串(含头不含尾)
        String str1 = stringBuffer.substring(2,5);
        System.out.println(str1);
    }
}

运行结果:
在这里插入图片描述
这里注意,截取到的字符串不再返回的是StringBuffer本身,而是一个新的字符串

String和StringBuffer

  1. 互换
    String–>StringBuffer
    (1)通过构造方法
    (2)通过append方法
    例如:
package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //定义一个字符串
        String str = "abc";
        //通过构造方法转换成StringBuffer类型
        StringBuffer stringBuffer = new StringBuffer(str);
        System.out.println(stringBuffer);//abc
        //通过append方法
        String str1 = "qwe";
        StringBuffer stringBuffer1 = new StringBuffer().append(str1);
        System.out.println(stringBuffer1);//qwe
    }
}

StringBuffer–>String
(1)通过构造方法
(2)通过toString方法
(3)使用subString方法
例如:

package org.westos.test;

public class Test2 {
    public static void main(String[] args) {
        //创建一个StringBuffer对象
        StringBuffer stringBuffer = new StringBuffer("abc");
        //通过构造方法
        String str = new String(stringBuffer);//abc
        //通过toString方法
        String str1 = stringBuffer.toString();//abc
        //通过substring方法
        String str2 = stringBuffer.substring(0);//abc
    }
}
  1. 区别
    String一旦创建,长度和内容都确定,不可改变,调用方法后返回新字符串
    StringBuffer创建之后,有初始长度,但后期可以改变,内容也可以改变,且在调用方法后返回本身

StringBuffer和StringBuilder

StringBuffer表示线程安全的可变字符序列,而StringBuilder表示线程不安全的可变字符序列,但它效率高
所以在单线程中,不存在安全问题,建议使用StringBuilder,实现会快些,而在多线程中就必须要使用StringBuffer了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值