java常用类StringBuffer

java常用类StringBuffer

由于String作为不可修改的对象,我们如果对字符串进行拼接操作,每次拼接都会构建一个新的String对象,即耗时又浪费空间,而StringBuffer这个类可以解决此问题。
StringBuffer又称为字符串缓冲区,即就是一个容器,容器中可以装很多字符串,能够对字符串进行添加、删除、替换等功能且不会产生新的未使用对象。

StringBuffer构造方法

public StringBuffer();   空参构造  构造一个其中不带字符的字符缓冲区,初始容量为16个字符。
public StringBuffer(int capacity);  构造一个不带字符,但具有指定初始容量的字符缓冲区。
public StringBuffer(String str);   构造一个字符缓冲区,并将其内容初始化为指定的字符串内容。
StringBuffer中两个统计长度方法:
public int capacity();  返回当前容量  理论值
public int length();  返回长度   实际值
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        StringBuffer sb = new StringBuffer();
        StringBuffer sb1 = new StringBuffer(1000);
        System.out.println(sb.capacity());
        System.out.println(sb1.capacity());
        StringBuffer sb2 = new StringBuffer("abc");
        System.out.println(sb2);
    }
}
结果是

在这里插入图片描述

StringBuffer添加方法

StringBuffer添加功能:
public StringBuffer append(String str);      可以将任意类型数据添加到字符串缓存区,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str);     在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        StringBuffer sb = new StringBuffer();
        sb.append("abc");
        System.out.println(sb);     
        sb.insert(2,"def");      //字符串的索引也和数组一样都是从左边开始0开始
        System.out.println(sb);
    }
}
结果是

在这里插入图片描述

StringBuffer删除方法

StringBuffer的删除功能:
public StringBuffer deletChart(int index);     删除指定位置的字符,并返回本身
public StringBuffer delet(int start,int end);     删除从指定位置开始到指定位置结束的内容,并返回本身
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        StringBuffer sb = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
        sb.deleteCharAt(3);
        System.out.println(sb);
        sb.delete(3,25);
        System.out.println(sb);
    }
}
结果是

在这里插入图片描述

StringBuffer替换和反转方法

StringBuffer替换和反转功能:
public StringBuffer replace(int start,int end,String str);    从start开始到end结束用指定的字符串来替换。
public StringBuffer reverse();      字符串反转
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        String  str="爱java,爱生活";
        StringBuffer sb = new StringBuffer(str);
        sb.replace(6,9,"爱未来");
        System.out.println(sb);
        sb.reverse();
        System.out.println(sb);
    }
}
结果是

在这里插入图片描述

StringBuffer和String相互转换

String类型转换StringBuffer类型
1.构造方法
2.通过append方法
3.通过insert方法
StringBuffer类型转换成String类型
1.使用substring方法
2.构造方法
3.通过toString方法
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        String str = "abc";
        //String------->StringBuffer
        StringBuffer sb = new StringBuffer(str);      //方式1
        StringBuffer sb2 = new StringBuffer().append(str);           //方式2
        StringBuffer sb3 = new StringBuffer().insert(0, str);        //方式3
        //StringBuffer------>String
        StringBuffer sb4 = new StringBuffer("hahaha");
        String s = new String(sb4);              //方式1
        String s2 = sb4.substring(0);              //方式2
        String s3 = sb4.toString();               //方式3
    }
}

猜你喜欢

转载自blog.csdn.net/m0_51717449/article/details/111239322