StringBuffer is a peer class of String that provides much of the functionality of strings. As you know, String represents fixed-length, immutable character sequences. In contrast, StringBuffer represents growable and writeable character sequences. StringBuffer may have characters and substrings inserted in the middle or appended to the end. StringBuffer will automatically grow to make room for such addition
StringBuffer Constructors
StringBuffer defines these four constructors:
StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
length( ) and capacity( )
The current length of a StringBuffer can be found via the length( ) method, while the total allocated capacity can be found through the capacity( ) method. They have the following general forms:
int length( )
int capacity( )
StringBuffer length vs. capacity
class StringBufferDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
System.out.println("buffer = " + sb);
System.out.println("length = " + sb.length());
System.out.println("capacity = " + sb.capacity());
}
}
buffer = Hello
length = 5
capacity = 21
append( )
The append( ) method concatenates the string representation of any other type of data to the end of the invoking StringBuffer object. It has several overloaded versions
Demonstrate append()
class appendDemo {
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);
}
}
Output
a = 42!
insert( )
The insert( ) method inserts one string into another. It is overloaded to accept values of all the simple types, plus Strings, Objects, and CharSequences. Like append( ), it calls String.valueOf( ) to obtain the string representation of the value it is called with. This string is then inserted into the invoking StringBuffer object. These are a few of its forms
StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)
Demonstrate insert()
class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
Output
I like Java!
reverse( )
Using reverse() to reverse a StringBuffer
class ReverseDemo {
public static void main(String args[]) {
StringBuffer s = new StringBuffer("abcdef");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}
output produced by the program
abcdef
fedcba
delete( ) and deleteCharAt( )
Demonstrate delete() and deleteCharAt()
class deleteDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.delete(4, 7);
System.out.println("After delete: " + sb);
sb.deleteCharAt(0);
System.out.println("After deleteCharAt: " + sb);
}
}
After delete: This a test.
After deleteCharAt: his a test.
replace( )
Demonstrate replace()
class replaceDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.replace(5, 7, "was");
System.out.println("After replace: " + sb);
}
}
Output
After replace: This was a test
substring( )
String substring(int startIndex)
String substring(int startIndex, int endIndex)
Additional StringBuffer Methods
Method | Description |
---|---|
StringBuffer appendCodePoint(int ch) | Appends a Unicode code point to the end of the invoking object. A reference to the object is returned. Added by J2SE 5. |
int codePointAt(int i) | Returns the Unicode code point at the location specified by i. Added by J2SE 5. |
int codePointBefore(int i) | Returns the Unicode code point at the location that precedes that specified by i. Added by J2SE 5. |
int codePointCount(int start, int end) | Returns the number of code points in the portion of the invoking String that are between start and end–1. Added by J2SE 5 |
int indexOf(String str) | Searches the invoking StringBuffer for the first occurrence of str. Returns the index of the match, or –1 if no match is found. |
int indexOf(String str, int startIndex) | Searches the invoking StringBuffer for the first occurrence of str, beginning at startIndex. Returns the index of the match, or –1 if no match is found. |
int lastIndexOf(String str) | Searches the invoking StringBuffer for the last occurrence of str. Returns the index of the match, or –1 if no match is found. |
int lastIndexOf(String str, int startIndex | Searches the invoking StringBuffer for the last occurrence of str, beginning at startIndex. Returns the index of the match, or –1 if no match is found |
int offsetByCodePoints(int start, int num) | Returns the index with the invoking string that is num code points beyond the starting index specified by start. Added by J2SE 5. |
CharSequence subSequence(int startIndex, int stopIndex) | Returns a substring of the invoking string, beginning at startIndex and stopping at stopIndex. This method is required by the CharSequence interface, which is now implemented by StringBuffer |
void trimToSize( ) | Reduces the size of the character buffer for the invoking object to exactly fit the current contents. Added by J2SE 5. |