J.E.D.I.
4.3.3 The StringBuffer Class
Once a String object is created, it can no longer be modified. A StringBuffer object is similar to a String object, except for the fact that the StringBuffer object is mutable or
can be modified. Whereas a String object is immutable, StringBuffer objects are mutable. Its length and content may be changed through some method calls.
Here are some of the methods in the StringBuffer class. Please refer to the Java API documentation.
StringBuffer Methods
public int capacity Returns the current capacity of this StringBuffer object.
public StringBuffer append- Appends the string representation of the argument to this StringBuffer object. Takes in
a single parameter which may be of these data types: boolean, char, char [], double, float, int, long, Object, String and StringBuffer. Still has another overloaded version.
public char charAtint index Returns the character located in the specified index.
public void getCharsint srcBegin, int srcEnd, char[] dst, int dstBegin Gets the characters from this object starting at the srcBegin index up to the srcEnd
index and copies these characters to the dst array starting at the dstBegin index. public StringBuffer deleteint start, int end
Deletes the characters within the specified range. public StringBuffer insertint offset, -
Inserts the string representation of the second argument at the specified offset. An overloaded method. Possible data types for the second argument: boolean, char, char
[], double, float, int, long, Object and String. Still has another overloaded version.
public int length Returns the number of characters in this StringBuffer object.
public StringBuffer replaceint start, int end, String str Replaces part of this object, as specified by the first two arguments, with the specified
string str. public String substringint start, int end
Returns the substring of this string starting at the specified start index up to the end index.
public String toString Converts this object to its string representation.
Table 9: Some methods of class StringBuffer
Introduction to Programming II Page 63
J.E.D.I.
The program below demonstrates the use of these methods. class StringBufferDemo {
public static void mainString args[] { StringBuffer sb = new StringBufferJonathan;
System.out.printlnsb = + sb; initial capacity is 16
System.out.printlncapacity of sb: + sb.capacity; System.out.printlnappend \O\ to sb: +
sb.appendO; System.out.printlnsb = + sb;
System.out.println3
rd
character of sb: + sb.charAt2;
char charArr[] = Hi XX.toCharArray; Need to add 1 to the endSrc index of getChars
sb.getChars0, 2, charArr, 3; System.out.printgetChars method: ;
System.out.printlncharArr; System.out.printlnInsert \jo\ at the 3rd cell: +
sb.insert2, jo; System.out.printlnDelete \jo\ at the 3rd cell: +
sb.delete2,4; System.out.printlnlength of sb: + sb.length;
System.out.printlnreplace: + sb.replace3, 9, Ong;
Need to add 1 to the endIndex parameter of substring System.out.printlnsubstring 1st two characters: +
sb.substring0, 3; System.out.printlnimplicit toString: + sb;
} }
Heres the output of the given program. Again, feel free to experiment with the code since the best way to learn these syntax is through actual use.
sb = Jonathan capacity of sb: 24
append O to sb: JonathanO sb = JonathanO
3rd character of sb: n getChars method: Hi Jo
Insert jo at the 3rd cell: JojonathanO Delete jo at the 3rd cell: JonathanO
length of sb: 9 replace: Jon Ong
substring 1st two characters: Jon implicit toString: Jon Ong
Introduction to Programming II Page 64
J.E.D.I.
4.4 The Wrapper Classes