StringBuilder转String:

    • public toString():通过toString()就可以实现把StringBuilder转换为String

    String转StringBuilder:

    • public StringBuilder(String s):通过构造方法就可以实现把String转换为StringBuilder

    代码演示:

    1. package test;
    2. public class Main {
    3. public static void main(String[] args) {
    4. StringBuilder s = new StringBuilder();
    5. s.append("WTY");
    6. //StringBuilder转String
    7. String s1 = s.toString();
    8. System.out.println(s1);
    9. System.out.println("------");
    10. String s2 = "Hello";
    11. StringBuilder s3 = new StringBuilder(s2);
    12. System.out.println(s3);
    13. }
    14. }

    运行结果:
    image.png