Append data to the end of a StringBuffer

ReturnMethodSummary
StringBufferappend(boolean b)Appends the boolean argument to the sequence.
StringBufferappend(char c)Appends the char argument to this sequence.
StringBufferappend(char[] str)Appends the char array argument to this sequence.
StringBufferappend(char[] str, int offset, int len)Appends a subarray of the char array argument to this sequence.
StringBufferappend(CharSequence s)Appends the CharSequence to this sequence.
StringBufferappend(CharSequence s, int start, int end)Appends a subsequence of the CharSequence to this sequence.
StringBufferappend(double d)Appends the double argument to this sequence.
StringBufferappend(float f)Appends the float argument to this sequence.
StringBufferappend(int i)Appends the int argument to this sequence.
StringBufferappend(long lng)Appends the long argument to this sequence.
StringBufferappend(Object obj)Appends the Object argument.
StringBufferappend(String str)Appends this character sequence.
StringBufferappend(StringBuffer sb)Appends the StringBuffer to this sequence.
StringBufferappendCodePoint(int codePoint)Appends the codePoint argument to this sequence.
StringBufferinsert(int offset, boolean b)Inserts the boolean argument into this sequence.
StringBufferinsert(int offset, char c)Inserts the char argument into this sequence.
StringBufferinsert(int offset, char[] str)Inserts the char array argument into this sequence.
StringBufferinsert(int index, char[] str, int offset, int len)Inserts a subarray of the str array argument into this sequence.
StringBufferinsert(int dstOffset, CharSequence s)Inserts CharSequence into this sequence.
StringBufferinsert(int dstOffset, CharSequence s, int start, int end)Inserts a subsequence of the specified CharSequence into this sequence.
StringBufferinsert(int offset, double d)Inserts the double argument into this sequence.
StringBufferinsert(int offset, float f)Inserts the float argument into this sequence.
StringBufferinsert(int offset, int i)Inserts the second int argument into this sequence.
StringBufferinsert(int offset, long l)Inserts the long argument into this sequence.
StringBufferinsert(int offset, Object obj)Inserts the Object argument into this character sequence.
StringBufferinsert(int offset, String str)Inserts str.

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[] argv) {
    StringBuffer sb = new StringBuffer();
    sb.append(true);
    sb.append("java2s.com");
    sb.append(1.2);
    sb.append('a');
    
    sb.insert(3,"JAVA@S.COM");
    System.out.println(sb.toString());
  }
}

The output:


truJAVA@S.COMejava2s.com1.2a

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!

The following sample program inserts strings:

 
public class Main {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("java2s.com");
    sb.insert(2, "like ");
    System.out.println(sb);
  }
}

The output of this example is shown here:


jalike va2s.com
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.