线程安全的可变字符串StringBuffer

用String做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的。为了解决这个问题,Java就提供了一个字符串缓冲区类StringBuffer:线程安全可变字符串

  • 构造方法
    • public StringBuffer()
    • public StringBuffer(int capacity) //指定容量的字符串缓冲区对象
    • public StringBuffer(String str)    //指定字符串内容的字符串缓冲区对象
public class StringBufferDemo {
    public static void main(String[] args) {
        // public StringBuffer():无参构造方法
        StringBuffer sb = new StringBuffer();
        System.out.println("sb:" + sb);
        System.out.println("sb.capacity():" + sb.capacity());
        System.out.println("sb.length():" + sb.length());
        System.out.println("--------------------------");

        // public StringBuffer(int capacity):指定容量的字符串缓冲区对象
        StringBuffer sb2 = new StringBuffer(50);
        System.out.println("sb2:" + sb2);
        System.out.println("sb2.capacity():" + sb2.capacity());
        System.out.println("sb2.length():" + sb2.length());
        System.out.println("--------------------------");

        // public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
        StringBuffer sb3 = new StringBuffer("hello");
        System.out.println("sb3:" + sb3);
        System.out.println("sb3.capacity():" + sb3.capacity());
        System.out.println("sb3.length():" + sb3.length());
    }
}

输出:
sb:
sb.capacity():16
sb.length():0
--------------------------
sb2:
sb2.capacity():50
sb2.length():0
--------------------------
sb3:hello
sb3.capacity():21
sb3.length():5
  • StringBuffer的常见功能
    • 添加功能
      • public StringBuffer append(String str)   //把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
      • public StringBuffer insert(int offset,String str)  //在指定位置任意类型数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
public class StringBufferDemo {
    public static void main(String[] args) {
        // 创建字符串缓冲区对象
        StringBuffer sb = new StringBuffer();

        StringBuffer sb2 = sb.append("hello");
        System.out.println("sb:" + sb);
        System.out.println("sb2:" + sb2);
        System.out.println(sb == sb2); // true

        // 一步一步的添加数据
        sb.append("hello");
        sb.append(true);
        sb.append(12);
        sb.append(34.56);
        System.out.println("sb:" + sb);

        // 链式编程
        sb.append("hello").append(true).append(12).append(34.56);
        System.out.println("sb:" + sb);

        // public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
        sb.insert(5, "world");
        System.out.println("sb:" + sb);
    }
}

输出:
sb:hello
sb2:hello
true
sb:hellohellotrue1234.56
sb:hellohellotrue1234.56hellotrue1234.56
sb:helloworldhellotrue1234.56hellotrue1234.56

调用append()返回字符串缓冲区自身,所以第9行返回true。而且append()可以把任意类型数据插入字符串缓冲区里。

  • 删除功能
    • public StringBuffer deleteCharAt(int index) //删除指定位置的字符,并返回本身
    • public StringBuffer delete(int start,int end)  //删除从指定位置开始指定位置结束的内容,并返回本身
  • 替换功能
    • public StringBuffer replace(int start,int end,String str) //从start开始到end用str替换,并返回本身
  • 截取功能
    • public String substring(int start) //从指定位置开始截取字符串,默认到末尾。返回String字符串
    • public String substring(int start,int end)
  • 反转功能
    • public StringBuffer reverse()
public class StringBufferDemo {
    public static void main(String[] args) {
        // 创建对象
        StringBuffer sb = new StringBuffer();
        sb.append("hello").append("world").append("java");

        // 删除功能
        // public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
        StringBuffer stringBuffer = sb.deleteCharAt(1);
        System.out.println("sb:" + sb);
        System.out.println(stringBuffer == sb);
        System.out.println("--------------------------");

        sb.delete(5, 10);
        System.out.println("sb:" + sb);
        System.out.println("--------------------------");

        // 需求:我要删除所有的数据
        sb.delete(0, sb.length());
        System.out.println("sb:" + sb);
        System.out.println("--------------------------");

        // 替换功能
        sb.append("hello").append("world").append("java");
        // public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
        stringBuffer = sb.replace(5, 10, "节日快乐");
        System.out.println("sb:" + sb);
        System.out.println(stringBuffer == sb);
        System.out.println("--------------------------");

        // 截取功能
        // public String substring(int start)
        String s = sb.substring(5);
        System.out.println("s:" + s);
        System.out.println("sb:" + sb);
        System.out.println("--------------------------");

        // public String substring(int start,int end)
        String ss = sb.substring(5, 9);
        System.out.println("ss:" + ss);
        System.out.println("sb:" + sb);
        System.out.println("--------------------------");

        //反转功能
        // public StringBuffer reverse()
        stringBuffer = sb.reverse();
        System.out.println(stringBuffer == sb);
        System.out.println("sb:" + sb);
    }
}

输出:
sb:hlloworldjava
true
--------------------------
sb:hllowava
--------------------------
sb:
--------------------------
sb:hello节日快乐java
true
--------------------------
s:节日快乐java
sb:hello节日快乐java
--------------------------
ss:节日快乐
sb:hello节日快乐java
--------------------------
true
sb:avaj乐快日节olleh

11、28、47行看出,拼加、删除、替换、反转后,返回原StringBuffer对象的引用。截取substring方法返回String类型,并且原StringBuffer对象内容不变

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不会叫的狼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值