站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Modifying String Buffers and String Builders - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Modifying String Buffers and String Builders

As you know, string buffers and string builders can change their content after they've been created. Both classes provide various methods for modifying their data. The following table summarizes the methods used to modify a string buffer. The same methods exist in the StringBuilder class, but return string builders instead of string buffers.

Methods for Modifying a String Buffer
Method Description
StringBuffer append(boolean)
StringBuffer append(char)
StringBuffer append(char[])
StringBuffer append(char[], int, int)
StringBuffer append(double)
StringBuffer append(float)
StringBuffer append(int)
StringBuffer append(long)
StringBuffer append(Object)
StringBuffer append(String)
Appends the argument to this string buffer. The data is converted to a string before the append operation takes place.
StringBuffer delete(int, int)
StringBuffer deleteCharAt(int)
Deletes the specified character(s) in this string buffer.
StringBuffer insert(int, boolean)
StringBuffer insert(int, char)
StringBuffer insert(int, char[])
StringBuffer insert(int, char[], int, int)
StringBuffer insert(int, double)
StringBuffer insert(int, float)
StringBuffer insert(int, int)
StringBuffer insert(int, long)
StringBuffer insert(int, Object)
StringBuffer insert(int, String)
Inserts the second argument into the string buffer. The first integer argument indicates the index before which the data is to be inserted. The data is converted to a string before the insert operation takes place.
StringBuffer replace(int, int, String)
void setCharAt(int, char)
Replaces the specified character(s) in this string buffer.
StringBuffer reverse() Reverses the sequence of characters in this string buffer.

You saw the append method in action in the StringsDemo program at the beginning of this section. Here's a program, InsertDemo, that uses the insert method to insert a string into a string buffer:

public class InsertDemo {
    public static void main(String[] args) {
        StringBuffer palindrome = new StringBuffer(
                        "A man, a plan, a canal; Panama.");
        palindrome.insert(15, "a cat, ");
        System.out.println(palindrome);
    }
}
The output from this program is still a palindrome:
A man, a plan, a cat, a canal; Panama.
With insert, you specify the index before which you want the data inserted. In the example, 15 specifies that "a cat, " is to be inserted before the first a in a canal. To insert data at the beginning of a string buffer, use an index of 0. To add data at the end of a string buffer, use an index equal to the current length of the string buffer or use append.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.