StringBuffer-字符串缓冲区

StringBuffer是字符串缓冲区(是一个容器)

特点:
  • 1,长度是可变化的。
  • 2,可以字节操作多个数据类型。
  • 3,最终会通过toString方法变成字符串。
1、存储

  • StringBuffer append();将制定数据作为参数添加到已有数据结尾处
  • StringBuffer insert(index,数据):可以将数据插入到指定index位置

2、删除

  • StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end
  • StringBuffer deleteCharAt(index):删除制定位置的字符

3、获取
  • char charAt(int index);获取指定索引位置的字符
  • int indexOf(String str);返回第一次出现的指定子字符串在该字符串中的索引
  • int lastIndexOf(String str):从尾巴开始
  • int length();
  • String substring(int start,int end);
4、修改

  • StringBuffer replace(start,end,string);
  • void setCharAt(int index,char ch);将给定索引处的字符设置为 ch。

5、反转

  • StringBuffer reverse();将此字符序列用其反转形式取代。

6、将缓冲区中指定数据存储到指定字符数组中

  • void getChars(int srcBegin,int serEnd,char[] dst,int dstBegin);将字符从此序列复制到目标字符数组 dst

JDK1.5版本之后出现了StringBuilder

  • StringBuffer是线程同步
  • StringBuilder是线程不同步

以后开发,建议使用StringBuilder
升级三个因素:

  • 1、提高效率
  • 2、简化书写
  • 3、提高安全性

代码示例:
public class StringBufferDemo01 {

    public static void main(String[] args) {

//        method_1();
//        method_2();
//        method_update();
//        method_delete();
//        method_add();
        draw(3,3);
    }

    public static void draw(int row,int col){
        StringBuilder sb = new StringBuilder();
        for(int x=0;x<row;x++){
            for(int y=0;y<col;y++){
                sb.append("*");
            }
            sb.append("\r\n");
        }
        System.out.println(sb.toString());
    }

    public static void method_add(){
        StringBuffer sb = new StringBuffer();
        StringBuffer sb1 = new StringBuffer(123);
        sb.append("abc").append(true).append(123);
        System.out.println("sb==sb1:"+(sb==sb1));

        sb.insert(1,"qq");//在索引为1的位置插入
        System.out.println(sb.toString());
    }

    public static void method_delete(){
        StringBuffer sb = new StringBuffer("abcdefg");
        System.out.println("删除前:"+sb.toString());
        sb.delete(1, 3);//删除,包含1不包含3
        System.out.println("删除后:"+sb.toString());

        StringBuilder sb1 = new StringBuilder("123456789");
        sb1.deleteCharAt(5);//删除指定位置的字符
        System.out.println("删除指定位置后:"+sb1.toString());
        sb1.delete(0, sb1.length());//清空缓冲区
        System.out.println("清空缓冲区:"+sb1);

    }

    public static void method_update(){
        StringBuffer sb = new StringBuffer("abcdefg");
        sb.replace(1, 4, "java");//替换
        System.out.println(sb.toString());
        sb.setCharAt(2, 'k');//设置
        System.out.println(sb.toString());
    }

    public static void method_2(){
        StringBuilder sb = new StringBuilder("abcdefgh");
        char[] chs = new char[6];
        //将字符串中的从索引1到4(不包含4)的字符复制到字符数组中,字符数组从索引1开始复制
        sb.getChars(1, 4, chs, 1);

        for(int x=0;x<chs.length;x++){
            System.out.println("chs["+x+"]="+chs[x]+";");
        }
    }

    public static void method_1(){
        StringBuilder sb = new StringBuilder();
        sb.append(new StringDemo01()).append("new StringDemo01()");
        sb.append("new StringDemo02()");
        System.out.println(sb.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值