Java StringBuilder append() Method Example

Java StringBuilder class has several overloaded versions. Here are all of its forms:
  1. StringBuilder append(boolean b) - Appends the string representation of the boolean argument to the sequence.
  2. StringBuilder append(char c) - Appends the string representation of the char argument to this sequence.
  3. StringBuilder append(char[] str) - Appends the string representation of the char array argument to this sequence.
  4. StringBuilder append(char[] str, int offset, int len) - Appends the string representation of a subarray of the char array argument to this sequence.
  5. StringBuilder append(CharSequence s) - Appends the specified CharSequence to this sequence.
  6. StringBuilder append(CharSequence s, int start, int end) - Appends a subsequence of the specified CharSequence to this sequence.
  7. StringBuilder append(double d) - Appends the string representation of the double argument to this sequence.
  8. StringBuilder append(float f) - Appends the string representation of the float argument to this sequence.
  9. StringBuilder append(int i) - Appends the string representation of the int argument to this sequence.
  10. StringBuilder append(long lng) - Appends the string representation of the long argument to this sequence.
  11. StringBuilder append(Object obj) - Appends the string representation of the Object argument.
  12. StringBuilder append(String str) - Appends the specified string to this character sequence.
  13. StringBuilder append(StringBuffer sb) - Appends the specified StringBuffer to this sequence.
  14. StringBuilder appendCodePoint(int codePoint) - Appends the string representation of the codePoint argument to this sequence.

Java StringBuilder append() Method Example

Let's demonstrate each append() method with examples. This program demonstrates the usage of above 14 append methods.
public class AppendExample {
 public static void main(String[] args) {
  // 13 append overloaded methods

  StringBuilder builder;
  // append String

  // Append String
  builder = new StringBuilder().append("guides");
  System.out.println("Append string : " + builder);
  
  // Append char
  builder = new StringBuilder().append('c');
  System.out.println("Append char : " + builder);
  // Append Object
  builder = new StringBuilder().append(new Object().getClass());
  System.out.println("Append Object : " + builder);
  
  // Append chars
  char[] chars = { 'j', 'a', 'v', 'a' };
  builder = new StringBuilder().append(chars);
  System.out.println("Append chars : " + builder);
  
  // Append charSequence
  builder = new StringBuilder().append("charSequence");
  System.out.println("Append charSequence : " + builder);
  
  // Append Double
  builder = new StringBuilder().append(10.0d);
  System.out.println("Append Double : " + builder);
  
  // Append Float
  builder = new StringBuilder().append(10.5f);
  System.out.println("Append Float : " + builder);
  
  // Append int
  builder = new StringBuilder().append(100);
  System.out.println("Append int : " + builder);
  
  // Append Boolean
  builder = new StringBuilder().append(true);
  System.out.println("Append Boolean : " + builder);
  
  // Append Long
  builder = new StringBuilder().append(100000);
  System.out.println("Append Long : " + builder);
  
  // Appends the specified StringBuffer to this sequence
  builder = new StringBuilder().append(new StringBuffer("stringbuffer"));
  System.out.println("Append StringBuffer : " + builder);
  
  // Appends the string representation of a subarray of the char array.
  builder = new StringBuilder().append(chars, 1, 3);
  System.out.println("Append subarray of the char array : " + builder);
  
  // Appends a subsequence of the specified CharSequence to this sequence.
  builder = new StringBuilder().append("javaguides", 0, 9);
  System.out.println("Append CharSequence : " + builder);
  
  // Appends the string representation of the codePoint argument to this
  // sequence.
  builder = new StringBuilder("javaguides").appendCodePoint(0);
  System.out.println("Append appendCodePoint : " + builder);

 }
}
Output:
Append string : guides
Append char : c
Append Object : class java.lang.Object
Append chars : java
Append charSequence : charSequence
Append Double : 10.0
Append Float : 10.5
Append int : 100
Append Boolean : true
Append Long : 100000
Append StringBuffer : stringbuffer
Append subarray of the char array : ava
Append CharSequence : javaguide
Append appendCodePoint : javaguides

Reference

Comments