010506
J. W. Rider

Class StringBuffer

In Java, a string buffer encapsulates a mutable sequence of characters. A string buffer is like a String, but the string buffer may be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence may be changed through method calls.

String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:

     x = "a" + 4 + "c"

is compiled to the equivalent of:

     x = new StringBuffer().append("a").append(4).append("c")
                           .toString()

which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.

The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.

For example, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would cause the string buffer to contain "startle", whereas z.insert(4, "le") would alter the string buffer to contain "starlet".

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

StringBuffer methods
StringBuffer append(X) Appends the string representation of the argument X to the string buffer. type of X = boolean, char, char[],double, float, int, long, Object, String
char charAt(int index) The specified character of the sequence currently represented by the string buffer, as indicated by the index argument, is returned.
StringBuffer delete(int start, int end) Removes the characters in a substring of this StringBuffer.
StringBuffer deleteCharAt(int index) Removes the character at the specified position in this StringBuffer (shortening the StringBuffer by one character).
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Characters are copied from this string buffer into the destination character array dst.
StringBuffer insert(int offset, X ) Inserts the string representation of the argument X into this string buffer. type of X = boolean, char, char[], double, float, int, long, Object, String
int length() Returns the length (character count) of this string buffer.
StringBuffer replace(int start, int end, String str) Replaces the characters in a substring of this StringBuffer with characters in the specified String.
StringBuffer reverse() The character sequence contained in this string buffer is replaced by the reverse of the sequence.
void setCharAt(int index, char ch) The character at the specified index of this string buffer is set to ch.
void setLength(int newLength) Sets the length of this String buffer.
String substring(int start) Returns a new String that contains a subsequence of characters currently contained in this StringBuffer.The substring begins at the specified index and extends to the end of the StringBuffer.
String substring(int start, int end) Returns a new String that contains a subsequence of characters currently contained in this StringBuffer.
String toString() Converts to a string representing the data in this string buffer.
1