Contact TLCC

Use StringBuffer class in Java to manipulate strings Click here to see all the Developer/Admin Tips

Date tip published:03/29/2004
Description:Java code is often used to manipulate and output strings. This is often done in servlets and Domino web agents when sending HTML to web browsers. Typically the String class is used along with the concatenation operator to create the string and to append several strings together. Another option is the StringBuffer class. The StringBuffer class is optimized to work with String data and has an append method to append two strings. This tip will demonstrate how to use the StringBuffer class.


To learn more about Java Programming use the following links:

Beginner Java Programming for Notes Domino 6
Intermediate Java Programming for Notes Domino 6
Java Programming Special - Domino 6
Java and WebSphere 5 Developer Package - Domino 6





Using the StringBuffer Class in Java

Java code is often used to manipulate and output strings. This is often done in servlets and Domino web agents when sending HTML to web browsers. Typically the String class is used along with the concatenation operator to create the string and to append several strings together. Another option is the StringBuffer class. The StringBuffer class is optimized to work with String data and has an append method to append two strings. This tip will demonstrate how to use the StringBuffer class.

Strings in Java (using the String class) are constants. Once they are initialized and populated the value and memory allocation is set (they are immutable, meaning they can not be changed.) If the String is changed in any way a new String object is created in memory for the new value.

The following code will append several values together and output a line of text to the system console.

String CustName = "John Smith";
String CustOrder = "Java Programming Course";
System.out.println(CustName + " ordered a " + CustOrder + ".");




StringBuffer Class

Another way to append Strings and perform String manipulation is the StringBuffer class. To create a StringBuffer object use the following:

    StringBuffer rb = new StringBuffer();

Then the append method can be used to append values together. The append method can accept any data type as input and converts the data to a String to be appended to the end of the value in the StringBuffer.
    rb.append("Hello").append(" ").append("World!");


The StringBuffer variable, rb, now holds the value of "Hello World!". To output the StringBuffer value use the toString() method:
    System.out.println(rb.append("Hello").append(" ").append("World!").toString());

To reset a StringBuffer (to use it somewhere else) set the length to zero using the setLength() method. This will "clear" out the value in the StringBuffer.
    rb.setLength(0);

Here are some other useful methods of the StringBuffer class.

delete() - used to delete characters
insert() - inserts characters
replace() - replaces characters
charAt() - returns the character at a specified index
deleteCharAt() - deletes the character at a specified index
setCharAt() - sets the character at a specified index


Below is the complete syntax for the contstructors and methods for the StringBuffer class from Sun J2SE 1.3 API documentation:


Constructor

Description

StringBuffer()

Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

StringBuffer(int)

Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.

StringBuffer(String)

Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string.



Method

Description

StringBuffer append(boolean)

Appends the string representation of the boolean argument to the string buffer.

StringBuffer append(char)

Appends the string representation of the char argument to this string buffer.

StringBuffer append(char[])

Appends the string representation of the char array argument to this string buffer.

StringBuffer append(char[], int, int)

Appends the string representation of a subarray of the char array argument to this string buffer.

StringBuffer append(double)

Appends the string representation of the double argument to this string buffer.

StringBuffer append(float)

Appends the string representation of the float argument to this string buffer.

StringBuffer append(int)

Appends the string representation of the int argument to this string buffer.

StringBuffer append(long)

Appends the string representation of the long argument to this string buffer.

StringBuffer append(Object)

Appends the string representation of the Object argument to this string buffer.

StringBuffer append(String)

Appends the string to this string buffer.

int capacity()

Returns the current capacity of the String buffer.

char charAt(int)

The specified character of the sequence currently represented by the string buffer, as indicated by the index argument, is returned.

StringBuffer delete(int, int)

Removes the characters in a substring of this StringBuffer.

StringBuffer deleteCharAt(int)

Removes the character at the specified position in this StringBuffer (shortening the StringBuffer by one character).

void ensureCapacity(int)

Ensures that the capacity of the buffer is at least equal to the specified minimum.

void getChars(int, int, char[], int)

Characters are copied from this string buffer into the destination character array dst.

StringBuffer insert(int, boolean)

Inserts the string representation of the boolean argument into this string buffer.

StringBuffer insert(int, char)

Inserts the string representation of the char argument into this string buffer.

StringBuffer insert(int, char[])

Inserts the string representation of the char array argument into this string buffer.

StringBuffer insert(int, char[], int, int)

Inserts the string representation of a subarray of the str array argument into this string buffer.

StringBuffer insert(int, double)

Inserts the string representation of the double argument into this string buffer.

StringBuffer insert(int, float)

Inserts the string representation of the float argument into this string buffer.

StringBuffer insert(int, int)

Inserts the string representation of the second int argument into this string buffer.

StringBuffer insert(int, long)

Inserts the string representation of the long argument into this string buffer.

StringBuffer insert(int, Object)

Inserts the string representation of the Object argument into this string buffer.

StringBuffer insert(int, String)

Inserts the string into this string buffer.

int length()

Returns the length (character count) of this string buffer.

StringBuffer replace(int, int, String)

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, char)

The character at the specified index of this string buffer is set to ch.

void setLength(int)

Sets the length of this String buffer.

String substring(int)

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, int)

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.