StringBuffer
s grow to the size needed, so
starting with a length of zero works correctly and does not
waste memory.
If speed is a concern, declare
a StringBuffer
somewhat larger than you might need.
This doesn't waste much space and puts fewer demands on the run-time system.
The capacity of a StringBuffer
is the current number of characters it can hold.
The capacity increases as needed.
The length of is the number of characters currently in the buffer.
As with arrays, StringBuffer
indexes start at 0 and go up to
length-1
.
Here are some StringBuffer
methods:
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 delete( int start, int end) | delete characters from start to end-1 |
StringBuffer deleteCharAt( int index) | delete 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 presently in the buffer. |
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. |
Look over the list of methods. Is there a method to reverse the order of characters?