StringBuilder insert and append

In this chapter you will learn:

  1. How to append data to the end of a StringBuilder
  2. How to chain append methods together
  3. How to insert data to the middle of a StringBuilder
  4. Combine append and insert methods

Append data to the end

StringBuilder has the following methods to append data to the end of a StringBuilder.
  • StringBuilder append(boolean b)
  • StringBuilder append(char c)
  • StringBuilder append(char[] str)
  • StringBuilder append(char[] str, int offset, int len)
  • StringBuilder append(CharSequence s)
  • StringBuilder append(CharSequence s, int start, int end)
  • StringBuilder append(double d)
  • StringBuilder append(float f)
  • StringBuilder append(int i)
  • StringBuilder append(long lng)
  • StringBuilder append(Object obj)
  • StringBuilder append(String str)
  • StringBuilder append(StringBuffer sb)
  • StringBuilder appendCodePoint(int codePoint)
public class Main {
  public static void main(String[] argv) {
    StringBuilder sb = new StringBuilder();
    sb.append("java2s.com");
    sb.append(123);/*j a v a 2 s  .c  om*/
    sb.append(1.23);
    sb.append(true);
    System.out.println(sb.toString());

  }
}

The output:

Chain append methods together

Each append methods from StringBuilder return an instance of current StringBuilder. We can chain the append methods together.

public class Main {
/* ja v  a2 s  .c om*/
  public static void main(String[] argv) {

    String s1 = "Hello" + ", " + "World";
    System.out.println(s1);

    // Build a StringBuilder, and append some things to it.
    StringBuilder sb2 = new StringBuilder();
    sb2.append("Hello");
    sb2.append(',');
    sb2.append(' ');
    sb2.append("World");

    StringBuilder sb3 = new StringBuilder().append("Hello").append(',').append(' ').append("World");
    System.out.println(sb3.toString());
  }
}

The output:

Insert data to the middle of a StringBuilder

With the following insert methods from StringBuilder we can insert value to the middle of a StringBuilder.

  • StringBuilder insert(int offset, boolean b)
  • StringBuilder insert(int offset, char c)
  • StringBuilder insert(int offset, char[] str)
  • StringBuilder insert(int index, char[] str, int offset, int len)
  • StringBuilder insert(int dstOffset, CharSequence s)
  • StringBuilder insert(int dstOffset, CharSequence s, int start, int end)
  • StringBuilder insert(int offset, double d)
  • StringBuilder insert(int offset, float f)
  • StringBuilder insert(int offset, int i)
  • StringBuilder insert(int offset, long l)
  • StringBuilder insert(int offset, Object obj)
  • StringBuilder insert(int offset, String str)

The following code inserts three lines of string to the StringBuilder. The new line escape string \n is used.

public class Main {
  public static void main(String[] a) {
    StringBuilder builder = new StringBuilder("Line 1\n");
//j  av  a  2  s  . c o m
    builder.append("Line 3\n");

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

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

  }
}

Output:

Combine append and insert methods

public class Main {
  public static void main(String[] argv) {
    StringBuilder sb = new StringBuilder();
    sb.append("java2s.com");
    sb.append(123);// j  av a 2s  . c o  m
    sb.append(1.23);
    sb.append(true);
    
    
    sb.insert(4,"JAVA2S.COM");
    System.out.println(sb.toString());

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to get the length for a StringBuilder
  2. How to get the capacity for a StringBuilder
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words