本帖最后由 dawei 于 2016-2-28 14:22 编辑
String和StringBuffer的转换
String-->StringBuffer
StringBuffer sb = new StringBuffer(String str);
StringBuffer-->String
String s = new String(StringBuffer sb);
String s1 = sb.toString();
代码演示:
public static void main(String[] args) {
//demo1();
StringBuffer sb = new StringBuffer("heima");
String s1 = new String(sb); //通过构造将StringBuffer转换为String
System.out.println(s1);
String s2 = sb.toString(); //通过toString方法将StringBuffer转换为String
System.out.println(s2);
String s3 = sb.substring(0, sb.length()); //通过截取子字符串将StringBuffer转换为String
System.out.println(s3);
}
private static void demo1() {
StringBuffer sb1 = new StringBuffer("heima"); //通过构造方法将字符串转换为StringBuffer对象
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer();
sb2.append("heima"); //通过append方法将字符串转换为StringBuffer对象
System.out.println(sb2);
}
|
|