StringBuilder的构造方法和成员方法
StringBuilder又叫字符串缓冲区,可以更高效率的处理字符串,常用两种成员方法。
public class DemoStringBuilder {
public static void main(String[] args) {
//StringBuilder的构造方法
StringBuilder b = new StringBuilder();
System.out.println(b);//空
StringBuilder b1 = new StringBuilder("hello");
System.out.println(b1);//hello
System.out.println("===========================");
//append方法,用于增加字符串内容
StringBuilder b2 = new StringBuilder();
b2.append(1);
b2.append("s");
b2.append(2.3).append('飞');
System.out.println(b2);
System.out.println("===========================");
//toString方法,String和StringBuilder的转换
String s = "asd";
System.out.println("s = " + s);
StringBuilder b3 = new StringBuilder(s);
b3.append("fg");
System.out.println("b3 = " + b3);
String s1 = b3.toString();
System.out.println("s1 = " + s1);
}
}
运行结果: