append

The append( ) method appends the string to the end.

StringBuffer append(boolean b)
Appends the boolean argument to the sequence.
StringBuffer append(char c)
Appends the char argument to this sequence.
StringBuffer append(char[] str)
Appends the char array argument to this sequence.
StringBuffer append(char[] str, int offset, int len)
Appends a subarray of the char array argument to this sequence.
StringBuffer append(CharSequence s)
Appends the CharSequence to this sequence.
StringBuffer append(CharSequence s, int start, int end)
Appends a subsequence of the CharSequence to this sequence.
StringBuffer append(double d)
Appends the double argument to this sequence.
StringBuffer append(float f)
Appends the float argument to this sequence.
StringBuffer append(int i)
Appends the int argument to this sequence.
StringBuffer append(long lng)
Appends the long argument to this sequence.
StringBuffer append(Object obj)
Appends the Object argument.
StringBuffer append(String str)
Appends this character sequence.
StringBuffer append(StringBuffer sb)
Appends the StringBuffer to this sequence.
StringBuffer appendCodePoint(int codePoint)
Appends the codePoint argument to this sequence.

public class Main {
  public static void main(String[] argv) {
    StringBuffer sb = new StringBuffer();
    sb.append(true);
    sb.append("java2s.com");
    sb.append(1.2);
    sb.append('a');
    System.out.println(sb.toString());
  }
}

The output:


truejava2s.com1.2a

public class Main {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    sb.append(true);
    sb.append('A');
    char[] carray = { 'a', 'b', 'c' };
    sb.append(carray);
    sb.append(carray, 0, 1);
    sb.append(3.5d);
    sb.append(2.4f);
    sb.append(45);
    sb.append(90000l);
    sb.append("That's all!");
    System.out.println(sb);
  }
}

String.valueOf( ) is called for each parameter to obtain its string representation.

The buffer itself is returned by each version of append( ).

This allows subsequent calls to be chained together, as shown in the following example:

 
public class Main {
  public static void main(String args[]) {
    String s;
    int a = 42;
    StringBuffer sb = new StringBuffer(40);
    s = sb.append("a = ").append(a).append("!").toString();
    System.out.println(s);
  }
}

The output of this example is shown here:


a = 42!
Home 
  Java Book 
    Essential Classes  

StringBuffer:
  1. Create StringBuffer object
  2. append
  3. StringBuffer capacity()
  4. charAt(int index): get the char at specified index
  5. delete(int start, int end) and deleteCharAt(int index)
  6. ensureCapacity( )
  7. getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  8. indexOf(String str)
  9. lastIndexOf(String str)
  10. StringBuffer length()
  11. Insert(): add data in the middle of a StringBuffer
  12. replace():replace a StringBuffer
  13. StringBuffer reverse()
  14. setCharAt(int index, char ch)
  15. setLength
  16. substring
  17. toString():Convert StringBuffer to String