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
Java Book
Essential Classes
StringBuffer:
- Create StringBuffer object
- append
- StringBuffer capacity()
- charAt(int index): get the char at specified index
- delete(int start, int end) and deleteCharAt(int index)
- ensureCapacity( )
- getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- indexOf(String str)
- lastIndexOf(String str)
- StringBuffer length()
- Insert(): add data in the middle of a StringBuffer
- replace():replace a StringBuffer
- StringBuffer reverse()
- setCharAt(int index, char ch)
- setLength
- substring
- toString():Convert StringBuffer to String