美文网首页
String&StringBuffer类转换

String&StringBuffer类转换

作者: Mr_chong | 来源:发表于2016-08-15 21:56 被阅读10次

一、String转化为StringBuffer类

方法一:使用StringBuffer类的构造方法,public StringBuffer(String str)
public class Person { public static void main(String[] args) { String str ="hello world"; StringBuffer buf = new StringBuffer(); buf.append(str); System.out.println(buf); } }
方法二:利用StringBuffer类的append()方法
public class Person { public static void main(String[] args) { String str ="hello world"; StringBuffer buf = new StringBuffer(str); System.out.println(buf); } }

二、StringBuffer转化为String

利用StringBuffer类的toString()方法完成
public class Person { public static void main(String[] args) { StringBuffer buf=new StringBuffer(); buf.append("hello world"); String str = buf.toString(); System.out.println(str); } }

相关文章

网友评论

      本文标题:String&StringBuffer类转换

      本文链接:https://www.haomeiwen.com/subject/sqmusttx.html