stringbuffer和string之间的转换
package com.ifenx8.study; public class Demo_String { /** A:String -- StringBuffer * a:通过构造方法 * b:通过append()方法 B:StringBuffer -- String * a:通过构造方法 * b:通过toString()方法 * c:通过subString(0,length); */ public static void main(String[] args) { demo();//string转换成stringbuffer demo2();//把stringbuffer转成string } public static void demo2() { //把stringbuffer转成string StringBuilder sb = new StringBuilder("ifenx8"); String s1 = new String(sb);//通过构造方法 System.out.println(s1); String s2 = sb.toString();//通过toString()方法 System.out.println(s2); String s3 = sb.substring(0,sb.length());//通过subString(0,length); System.out.println(s3); } public static void demo() { //string转换成stringbuffer StringBuilder sb = new StringBuilder("ifenx8");//把string字符串转换成stringbuffer System.out.println(sb); StringBuilder sb1 = new StringBuilder(); sb1.append("ifenx8.com"); System.out.println(sb1); } }
评论前必须登录!
注册