public class Demo6_StringBuffer {
/**StringBuffer和String的相互转换
*String -- StringBuffer
*通过构造方法
*通过append()方法
*StringBuffer -- String
*通过构造方法
*通过toString()方法
*通过substring(0,length) //这里直接用substring(0)不就行了吗?
*/
public static void main(String[] args) {
//demo1();
StringBuffer sb = new StringBuffer("heima");
String s1 = new String(sb);
String s2 = sb.toString();
String s3 = sb.substring(0);
String s4 = sb.substring(0, sb.length());
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
private static void demo1() {
StringBuffer sb = new StringBuffer("heima");
System.out.println(sb);
StringBuffer sb2 = new StringBuffer();
sb2.append("heima");
System.out.println(sb2);
}
}
|
|