StringBuffer
s grow to the size needed, so
starting with a length of zero works correctly and does not
waste memory.
Usually, if program execution speed is a concern, you should declare
a StringBuffer
to be just somewhat larger than you might need.
This doesn't waste much space and puts few demands on the run-time system.
As with arrays, StringBuffer
indexes start at 0 and go up to
length-1
.
Some StringBuffer
methods are:
StringBuffer Methods | |
---|---|
StringBuffer append( char c ) | append c to the end of the StringBuffer |
StringBuffer append( int i ) | convert i to characters, then append them to the end of the StringBuffer |
StringBuffer append( long L ) | convert L to characters, then append them to the end of the StringBuffer |
StringBuffer append( float f ) | convert f to characters, then append them to the end of the StringBuffer |
StringBuffer append( double d ) | convert d to characters, then append them to the end of the StringBuffer |
StringBuffer append( String s ) | append the characters in s to the end of the StringBuffer |
int capacity() | return the current capacity (capacity will grow as needed). |
char charAt( int index ) | get the character at index. |
StringBuffer insert( int index, char c) | insert character c at index (old characters move over to make room). |
StringBuffer insert( int index, String st) | insert characters from st starting at position i. |
StringBuffer insert( int index, int i) | convert i to characters, then insert them starting at index. |
StringBuffer insert( int index, long L) | convert L to characters, then insert them starting at index. |
StringBuffer insert( int index, float f) | convert f to characters, then insert them starting at index. |
StringBuffer insert( int index, double d) | convert d to characters, then insert them starting at index. |
int length() | return the number of characters. |
StringBuffer reverse() | Reverse the order of the characters. |
void setCharAt( int index, char c) | set the character at index to c. |
String toString() | return a String object containing the characters in the StringBuffer. |