Using the StringBuffer Class

   

Using the StringBuffer Class

The StringBuffer class enables you to create String objects that can be changed in various ways, unlike the String class, which are immutable. When you modify a string of the StringBuffer class, you're not creating a new String object, but rather operating directly on the original string itself. For this reason, the StringBuffer class offers a different set of methods than the String class, all of which operate directly on the buffer that contains the string.

Creating a StringBuffer Object

The StringBuffer class offers several constructors that enable you to construct a StringBuffer object in various ways. Those constructors look like this:

 StringBuffer() StringBuffer(int length) StringBuffer(String str) 

These constructors create an empty StringBuffer, an empty StringBuffer of the given length, and a StringBuffer from a String object (or string literal), respectively.

Appending to a StringBuffer

You saw earlier that the concat method or the "+" operator can be used to concatenate string objects. To append strings using a StringBuffer, you just pass the string to be appended as an argument to the append method on the StringBuffer Class. The following code shows how easy it is to concatenate multiple String objects:

 StringBuffer buf = new StringBuffer(); buf.append( "This is " ); buf.append( "another way " ); buf.append( "to join " ); buf.append( 5 ); buf.append( " strings together" ); 

Notice that you can also append numbers with the strings. In fact, the StringBuffer class has an append method that supports all the Java primitives. A neat feature about the append method is that you can chain multiple append methods together like this:

 StringBuffer buf = new StringBuffer(); buf.append( "This is another " ).append( "StringBuffer object" ); 

Note

When you use the "+" operator to concatenate String objects, the compiler actually uses a StringBuffer internally to perform the concatenation. Whether you use a "+" operator or a StringBuffer instance should depend on how many strings you are appending and which way makes it easier to read for the reader of the code.


Converting a StringBuffer Object to a String

To produce the Result string from the StringBuffer instance, just call the toString method on the StringBuffer instance like this:

 StringBuffer buf = new StringBuffer(); buf.append( "This is " ); buf.append( "another way " ); buf.append( "to join " ); buf.append( 5 ); buf.append( " strings together" ); return buf.toString(); 

You've already had some experience with string extraction when you learned about the String class. The StringBuffer class has two of the same methods for accomplishing this task. Those methods are charAt and getChars, both of which work similarly to the String versions. Here's an example of using charAt:

 StringBuffer str = new StringBuffer("String buffer"); char ch = str.charAt(5); 

And here's an example of using getChars:

 StringBuffer str = new StringBuffer("String buffer"); char ch[] = new char[20]; str.getChars(7, 10, ch, 0); 

In addition, you can also obtain a subString from a StringBuffer, just like with the String class as shown in the next example:

 StringBuffer str = new StringBuffer("String buffer"); String buffer = str.subString(8); String rin = str.subString(3,6); 

Manipulating a StringBuffer Object

You can modify the string that's stored in a StringBuffer object in several ways. Unlike the string modification methods in the String class, which create a new string, the methods in the StringBuffer class work directly on the buffer in which the original string is stored. The first thing you can do with a string buffer is set its length. You do this by calling the setLength method:

 StringBuffer str = new StringBuffer("String buffer"); str.setLength(40); 

This method's single argument is the new length. If the new length is greater than the old length, both the string and buffer length are increased, with the additional characters being filled with zeros. If the new length is smaller than the old length, characters are chopped off the end of the string, but the buffer size remains the same.

If you want to be guaranteed a specific buffer size, you can call the ensureCapacity method, like this:

 StringBuffer str = new StringBuffer("String buffer"); str.ensureCapacity(512); 

The ensureCapacity method's argument is the new capacity for the buffer.

You can change a character in the string buffer by calling the setCharAt method:

 StringBuffer str = new StringBuffer("String buffer"); str.setCharAt(3, 'X'); 

The setCharAt method's arguments are the index of the character to change and the new character. In the preceding example, the string buffer becomes StrXng buffer. You can also replace sections of the String with Strings or other native types using the replace methods. The set of replace methods works similar to setCharAt, only instead of requiring a character, you can replace sections of the other string types as well. Notice in the following example, that with insert you must provide the beginning and ending index of the characters to replace, instead of just the single character index you need with setCharAt:

 StringBuffer str = new StringBuffer("String buffer"); str.replace(3,6, 'XYZ'); 

You can add characters to the end of the string with the append method and insert characters anywhere in the string with the insert method. Both of these methods come in several versions that enable you to handle many different types of data. To add a character version of an integer to the end of the string, for example, do something like this:

 StringBuffer str = new StringBuffer("String buffer"); int value = 15; str.append(value); 

After this code executes, str contains String buffer15. Similarly, you insert characters like this:

 StringBuffer str = new StringBuffer("String buffer"); int value = 15; str.insert(6, value); 

This code results in a string of String15 buffer. The two arguments in the previous version of insert are the index at which to insert the characters and the data object to insert.

You can also delete characters from the StringBuffer using deleteCharAt and the delete method. deletCharAt requires the index of the character you want to delete, whereas delete() requires the start (inclusive) index and the end (exclusive) index, as shown in the next example:

 StringBuffer str = new StringBuffer("String buffer"); str.deleteCharAt(6); str.delete(3,6); 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net