StringBuilder转String:
- public toString():通过toString()就可以实现把StringBuilder转换为String
String转StringBuilder:
- public StringBuilder(String s):通过构造方法就可以实现把String转换为StringBuilder
代码演示:
package test;
public class Main {
public static void main(String[] args) {
StringBuilder s = new StringBuilder();
s.append("WTY");
//StringBuilder转String
String s1 = s.toString();
System.out.println(s1);
System.out.println("------");
String s2 = "Hello";
StringBuilder s3 = new StringBuilder(s2);
System.out.println(s3);
}
}
运行结果: