StringBuilder Class

Package: java.lang

StringBuilder represents a mutable sequence of characters. That is, a string that can be changed. This sets it apart from the String class, which represents an immutable sequence of characters. StringBuilder can be more efficient than String in applications that do a lot of string maniuplations.

TechnicalStuff.eps The StringBuilder class has a nearly identical twin called the StringBuffer class. Both classes have the same methods and perform the same string manipulations. The only difference is that whereas the StringBuffer class is safe to use in applications that work with multiple threads, StringBuilder is not safe for threaded applications but is more efficient than StringBuffer. As a result, you should use the StringBuffer class instead of the StringBuilder class if your application uses threads.

Constructors

Constructor

Description

StringBuilder()

Creates a new empty StringBuilder.

String(String str)

Creates a new StringBuilder object whose value is identical to str.

Methods

Method

Description

append(primitiveType)

Appends the string representation of the primitive type to the end of the string.

append(Object obj)

Calls the object’s toString method and appends the result to the end of the string.

append(CharSequence seq)

Appends the string to the end of the StringBuilder’s string value. The parameter can be a String, StringBuilder, or String Buffer.

char charAt(int index)

Returns the character at the specified position in the string.

delete(int start, int end)

Deletes characters starting with the first int and ending with the character before the second int.

deleteCharAt(int index)

Deletes the character at the specified position.

ensureCapacity(int min)

Ensures that the capacity of String-Builder is at least equal to the int value; increases the capacity if necessary.

int capacity()

Returns the capacity of this StringBuilder.

int indexOf(String str)

Returns the index of the first occurrence of the specified string. If the string doesn’t appear, returns –1.

int indexOf(String str, int start)

Returns the index of the first occurrence of the specified string, starting the search at the specified index position. If the string doesn’t appear, returns –1.

insert(int index, primitiveType )

Inserts the string representation of the primitive type at the point specified by the int argument.

insert(int index, Object obj)

Calls the toString method of the Object parameter and then inserts the resulting string at the point specified by the int argument.

insert(int index, CharSequence seq)

Inserts the string at the point specified by the int argument. The second parameter can be a String, StringBuilder, or String Buffer.

int lastIndexOf(String str)

Returns the index of the last occurrence of the specified string. If the string doesn’t appear, returns –1.

int lastIndexOf(String str, int start)

Returns the index of the last occurrence of the specified string, starting the search at the specified index position. If the string doesn’t appear, returns –1.

int length()

Returns the length of this string.

replace(int start, int, end String str)

Replaces the substring indicated by the first two parameters with the string provided by the third parameter.

reverse()

Reverses the order of characters.

setCharAt(int index, char chr)

Sets the character at the specified position to the specified character.

setLength(int len)

Sets the length of the string. If that length is less than the current length, the string is truncated; if it’s greater than the current length, new characters — hexadecimal zeros — are added.

String substring(int start)

Extracts a substring, beginning at the position indicated by the int parameter and continuing to the end of the string.

String substring(int start, int end)

Extracts a substring, beginning at the position indicated by the first parameter and ending at the position one character before the value of the second parameter.

String toString()

Returns the current value as a String.

String trimToSize()

Reduces the capacity of the StringBuffer to match the size of the string.

Creating a StringBuilder object

You can’t assign string literals directly to a StringBuilder object like you can with a String object. The StringBuilder class, however, has a constructor that accepts a String as a parameter. So to create a StringBuilder object, you use a statement such as this:

StringBuilder sb = new StringBuilder(“Today is the day!”);

Internally, a StringBuilder object maintains a fixed area of memory where it stores a string value. This area of memory is the buffer. The string held in this buffer doesn’t have to use the entire buffer. As a result, a StringBuilder object has both a length and a capacity. The length represents the current length of the string maintained by the StringBuilder, and the capacity represents the size of the buffer itself. Note that the length can’t exceed the capacity.

When you create a StringBuilder object, initially the capacity is set to the length of the string plus 16. The StringBuilder class automatically increases its capacity whenever necessary, so you don’t have to worry about exceeding the capacity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset