(60)StringBuffer:特点、存储、删除、获取、修改

StringBuffer是字符串缓冲区,是一个容器。
StringBuffer的特点:
1:长度是可变化的(数组也是容器,但长度固定)
2:可以操作多个数据类型(数组只能一种数据类型)
3:最终通过toString方法编程字符串
C:create U:update R: read D:delete (CURD)

JDK1.5版本之后出现了StringBuilder
StringBuffer是线程同步
StringBuilder是线程不同步
在单线程开发中使用StringBulider,因为StringBuffer同步必含锁的判断,效率较低
在多线程中建议使用StringBuffer

Java升级的三个因素:
1:提高效率
2:简化书写
3:提高安全性
一.存储

①添加: public StringBuffer append():将指定的数据作为参数添加到已有数据的结尾处
参数为基本数据类型,其中参数没有byte,short 但是也可以传入此类的值,因为有int自动类型提升
注意它和String区别:
String是一旦初始化,这个对象就不变了,在方法调用中返回一个新的对象,设置一个变量来接收这个新的变量。
StringBuffer是个容器,它返回的还是容器,是这个对象

public static void main(String[] args) {
        StringBuffer sb=new StringBuffer();
        StringBuffer sb1=sb.append(34);
        sop("sb==sb1?"+(sb==sb1));//true(说明指向都是这个容器)既然指向相同,并没有像String创建一个新的对象,仍然是原来的对象,所以不用创建一个新对象来接收。操作的都是同一个对象
        sb.append("abc").append(true).append(34);   

②插入
StringBuffer insert(index,任意类型)

  //插入:在a后面插入kk
        sb.insert(1, "kk");
        sop(sb.toString());

        //在不存在的角标处插入:抛出异常IndexOutOfBoundsException
        sb.insert(100, "kk");
        sop(sb.toString());

二.删除
StringBuffer delete(int start,end):包含头,不包含尾
StringBuffer deleteCharAt(index):删除特定位置上的字符

public static void method_delete() {
        StringBuffer sb=new StringBuffer("abcdefg");
        //删除一段
        sb.delete(2, 5);
        sop(sb.toString());//abfg
        //删除特定位置上的字符
        sb.deleteCharAt(2);
        sop(sb.toString());//abg
        //清空缓冲区
        sb.delete(0, sb.length());
        sop("哈哈"+sb.toString()+"哈哈");
    }

三.获取
public char charAt(int index):获取特定位置的字符
public int indexOf(String str):此方法相当于this.toString().startsWith(str, k)
若str在容器中存在,则返回开始下标;若不存在,返回-1
public int length()
public String substring(int start, int end)

public static void method_get() {
        StringBuffer sb=new StringBuffer("abcdefghijk");
        //获取特定位置的字符
        char ch=sb.charAt(2);
        sop(ch);
        //获取特定字符串开始的位置
        int i=sb.indexOf("ef");
        sop(i);
        //获取子字符串
        String s=sb.substring(2, 6);//substring返回字符串,所以输出不用toString
        sop(s);
    }

四.修改
①替换一段:public StringBuffer replace(int start, int end, String str)注意:start、end和str长度没有关系。
②替换某个字符:public void setCharAt(int index, char ch):注意返回值为void
③反转:public StringBuffer reverse():反转
④将容器中指定数据存入指定字符数组中:public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
参数:
srcBegin - start copying at this offset. (src:是source的简写 “源”)
srcEnd - stop copying at this offset.【end-1】
dst - the array to copy the data into.
dstBegin - offset into dst. 【des:destination”目的”】

public static void method_replace() {
        StringBuffer sb=new StringBuffer("helloworld");
        sb.replace(5, 10, "java");//不包含尾
        sop(sb.toString());

        sb.setCharAt(3, 'y');+
        sop(sb.toString());

        //反转
        sb.reverse();
        sop(sb.toString());

        //将容器中某段数据复制到字符数组中
        String s1=new String("abcdefg");
        char[] ch= {'h','i','j','k','l'};
        s1.getChars(2, 6, ch, 1);
        String s2=new String(ch);//将字符数组转化为字符串,就不用for循环输出了
        sop(s2)
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值