如果一个字符串需要经常被改变,就需要使用StringBuffer类。(String类型的变量一旦声明就很难改变,若想改变,必须改变引用地址)!
在程序书中使用append方法可以进行字符串的连接操作。
package Sep22;
public class StringBufferDemo01 {
public static void main(String[] args) {
StringBuffer buf=new StringBuffer();
buf.append("Hello");//利用append添加内容
buf.append(" World!").append("!!!!");//连续添加内容
buf.append("\n");
buf.append("数字:").append(1).append("\n");
System.out.println(buf);
}
}
Hello World!!!!! 数字:1
可以直接使用insert()方法。
package Sep22;
public class StringBufferDemo03 {
public static void main(String[] args) {
StringBuffer buf=new StringBuffer();
buf.append(" wodld");
buf.insert(0, "hello");
System.out.println(buf);
}
}
hello wodld
原文:http://www.cnblogs.com/BoscoGuo/p/5896405.html