常用方法
public StringBuffer append(String str)
可以把任意类型添加到字符串缓冲区里面,并返回字符串缓冲区本身
StringBuffer a=new StringBuffer("abc");
a.append("d");
public StringBuffer insert (int offset,String str)
在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
String a="acd";
StringBuffer b= new StringBuffer(a);
b.insert(1,"b");
System.out.println(b);
//结果 abcd
public StringBuffer deleteCharAt (int index)
删除指定位置的字符,并返回本身
b.deleteCharAt(1);
System.out.println(b);
删除一个区间内的字符
public StringBuffer delete(int a ,int b)范围左开右闭。
StringBuffer b= new StringBuffer("a12345678bcdefgfijklmnopqrstuvwxyz");
b.delete(1,9);
System.out.println(b);
结果:abcdefgfijklmnopqrstuvwxyz
public StringBuffer replace(int start,int end,String str)
从start开始到end用str替换
StringBuffer b= new StringBuffer("a12345678bcdefgfijklmnopqrstuvwxyz");
b.replace(1,9,"13908862319");
System.out.println(b);
结果:a13908862319bcdefgfijklmnopqrstuvwxyz
public StringBuffer reverse()
字符串反转
StringBuffer b= new StringBuffer("a12345678bcdefgfijklmnopqrstuvwxyz");
b.replace(1,9,"13908862319");
b.reverse();
System.out.println(b);
结果:zyxwvutsrqponmlkjifgfedcb91326880931a
public String substring(int start)
从指定位置截取到末尾
StringBuffer b= new StringBuffer("a12345678bcdefgfijklmnopqrstuvwxyz");
b.replace(1,9,"13908862319");
System.out.println(b.substring(12));
结果:bcdefgfijklmnopqrstuvwxyz
public String substring(int start,int end)
截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
StringBuffer b= new StringBuffer("a12345678bcdefgfijklmnopqrstuvwxyz");
b.replace(1,9,"13908862319");
System.out.println(b.substring(1,12));
结果:13908862319
public void trimToSize()
该方法的作用是将StringBuffer对象的中存储空间缩小到和字符串长度一样的长度,减少空间的浪费。
public int capacity()
返回当前容量,理论值
public int length()
返回长度(字符数),实际值
StringBuilder数组的创建
StringBuffer[] buffers = new StringBuffer[3];
for(int i=0;i<buffers.length ;i++){
buffers[i] = new StringBuffer[];
}
或者
StringBuffer[] buffers = { new StringBuffer() ,new StringBuffer(),new StringBuffer()};