toString():Convert StringBuilder to String

String toString()
Returns a string representing the data in this sequence.

public class Main {
  public static void main(String[] argv) {
    StringBuilder sb = new StringBuilder();
    sb.append("java2s.com");

    System.out.println(sb.toString());
  }
}

The output:


java2s.com

public class Main {
  public static void main(String[] a) {
    StringBuilder builder = new StringBuilder("Line 1\n");

    builder.append("Line 3\n");

    String lineToInsert = "Line 2\n";
    
    builder.insert(0, lineToInsert);

    System.out.println(builder.toString());

  }
}

Output:


Line 2
Line 1
Line 3
Home 
  Java Book 
    Essential Classes  

StringBuilder:
  1. StringBuilder
  2. StringBuilder Constructor
  3. append(char c)
  4. StringBuilder capacity()
  5. charAt()
  6. delete(int start, int end)
  7. deleteCharAt
  8. StringBuilder length()
  9. StringBuilder reverse()
  10. setCharAt
  11. Sub string from StringBuilder
  12. toString():Convert StringBuilder to String