Creates StringBuilder objects, append and insert text to them. Use the \n character for line breaks. : StringBuilder « Development Class « Java






Creates StringBuilder objects, append and insert text to them. Use the \n character for line breaks.

 

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());

  }
}
/*
Line 2
Line 1
Line 3

*/

   
  








Related examples in the same category

1.Check the capacity of StringBuilder object
2.Remove substring from StringBuilder
3.Use a StringBuilder to Mark Vowel
4.Deletes text from the StringBuilder object
5.StringBuilderDemo: construct the same String three different waysStringBuilderDemo: construct the same String three different ways
6.Java 1.5 (5.0) new features: StringBuilderJava 1.5 (5.0) new features: StringBuilder
7.Palindrome with StringBuilderPalindrome with StringBuilder
8.StringBuilder.reverseStringBuilder.reverse