用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对象内容不变。