http://www.verejava.com/?id=16993039377589
/**
知识点: 可变字符串对象 StringBuffer
1. 什么是StringBuffer
2. 为什么用StringBuffer
一般用在很多字符串相连接的情况
3. StringBuffer的操作和应用
*/
public class TestStringBuffer {
public static void main(String[] args) {
String str = "";
//实例化一个空的字符缓冲区
StringBuffer strBuffer = new StringBuffer();
for (int i = 0; i < 100; i++) {
str += i + ",";
strBuffer.append(i).append(",");
}
//输出字符缓冲区的字符串
System.out.println(strBuffer.toString());
//1. 字符串追加
StringBuffer buffer = new StringBuffer();
buffer.append("How");
buffer.append(" your");
//2. 字符串的输出
System.out.println(buffer.toString());
//3. 字符串的替换
buffer.replace(4, 9, "you");
System.out.println(buffer.toString());
//4. 字符串插入索引为3的后面
buffer.insert(3, " are");
System.out.println(buffer.toString());
//5. 字符串的截取
System.out.println(buffer.substring(0, 4));
}
}