Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.lang
Class StringBuilder  view StringBuilder download StringBuilder.java

java.lang.Object
  extended byjava.lang.StringBuilder
All Implemented Interfaces:
CharSequence, java.io.Serializable

public final class StringBuilder
extends Object
implements java.io.Serializable, CharSequence

StringBuilder represents a changeable String. It provides the operations required to modify the StringBuilder, including insert, replace, delete, append, and reverse. It like StringBuffer, but is not synchronized. It is ideal for use when it is known that the object will only be used from a single thread.

StringBuilders are variable-length in nature, so even if you initialize them to a certain size, they can still grow larger than that. Capacity indicates the number of characters the StringBuilder can have in it before it has to grow (growing the char array is an expensive operation involving new).

Incidentally, compilers often implement the String operator "+" by using a StringBuilder operation:
a + b
is the same as
new StringBuilder().append(a).append(b).toString().

Classpath's StringBuilder is capable of sharing memory with Strings for efficiency. This will help when a StringBuilder is converted to a String and the StringBuilder is not changed after that (quite common when performing string concatenation).

Since:
1.5

Field Summary
(package private)  int count
          Index of next available character (and thus the size of the current string contents).
private static int DEFAULT_CAPACITY
          The default capacity of a buffer.
private static long serialVersionUID
          For compatability with Sun's JDK
(package private)  char[] value
          The buffer.
 
Constructor Summary
StringBuilder()
          Create a new StringBuilder with default capacity 16.
StringBuilder(CharSequence seq)
          Create a new StringBuilder with the characters in the specified CharSequence.
StringBuilder(int capacity)
          Create an empty StringBuilder with the specified initial capacity.
StringBuilder(String str)
          Create a new StringBuilder with the characters in the specified String.
 
Method Summary
 StringBuilder append(boolean bool)
          Append the String value of the argument to this StringBuilder.
 StringBuilder append(char ch)
          Append the char to this StringBuilder.
 StringBuilder append(char[] data)
          Append the char array to this StringBuilder.
 StringBuilder append(char[] data, int offset, int count)
          Append part of the char array to this StringBuilder.
 StringBuilder append(CharSequence seq)
          Append the characters in the CharSequence to this buffer.
 StringBuilder append(CharSequence seq, int start, int end)
          Append some characters from the CharSequence to this buffer.
 StringBuilder append(double dnum)
          Append the String value of the argument to this StringBuilder.
 StringBuilder append(float fnum)
          Append the String value of the argument to this StringBuilder.
 StringBuilder append(int inum)
          Append the String value of the argument to this StringBuilder.
 StringBuilder append(long lnum)
          Append the String value of the argument to this StringBuilder.
 StringBuilder append(Object obj)
          Append the String value of the argument to this StringBuilder.
 StringBuilder append(String str)
          Append the String to this StringBuilder.
 StringBuilder append(StringBuffer stringBuffer)
          Append the StringBuilder value of the argument to this StringBuilder.
 StringBuilder appendCodePoint(int code)
          Append the code point to this StringBuilder.
 int capacity()
          Get the total number of characters this StringBuilder can support before it must be grown.
 char charAt(int index)
          Get the character at the specified index.
 int codePointAt(int index)
          Get the code point at the specified index.
 int codePointBefore(int index)
          Get the code point before the specified index.
 int codePointCount(int beginIndex, int endIndex)
          Returns the number of Unicode code points in the specified sub sequence.
 StringBuilder delete(int start, int end)
          Delete characters from this StringBuilder.
 StringBuilder deleteCharAt(int index)
          Delete a character from this StringBuilder.
 void ensureCapacity(int minimumCapacity)
          Increase the capacity of this StringBuilder.
 void getChars(int srcOffset, int srcEnd, char[] dst, int dstOffset)
          Get the specified array of characters.
 int indexOf(String str)
          Finds the first instance of a substring in this StringBuilder.
 int indexOf(String str, int fromIndex)
          Finds the first instance of a String in this StringBuilder, starting at a given index.
 StringBuilder insert(int offset, boolean bool)
          Insert the String value of the argument into this StringBuilder.
 StringBuilder insert(int offset, char ch)
          Insert the char argument into this StringBuilder.
 StringBuilder insert(int offset, char[] data)
          Insert the char[] argument into this StringBuilder.
 StringBuilder insert(int offset, char[] str, int str_offset, int len)
          Insert a subarray of the char[] argument into this StringBuilder.
 StringBuilder insert(int offset, CharSequence sequence)
          Insert the CharSequence argument into this StringBuilder.
 StringBuilder insert(int offset, CharSequence sequence, int start, int end)
          Insert a subsequence of the CharSequence argument into this StringBuilder.
 StringBuilder insert(int offset, double dnum)
          Insert the String value of the argument into this StringBuilder.
 StringBuilder insert(int offset, float fnum)
          Insert the String value of the argument into this StringBuilder.
 StringBuilder insert(int offset, int inum)
          Insert the String value of the argument into this StringBuilder.
 StringBuilder insert(int offset, long lnum)
          Insert the String value of the argument into this StringBuilder.
 StringBuilder insert(int offset, Object obj)
          Insert the String value of the argument into this StringBuilder.
 StringBuilder insert(int offset, String str)
          Insert the String argument into this StringBuilder.
 int lastIndexOf(String str)
          Finds the last instance of a substring in this StringBuilder.
 int lastIndexOf(String str, int fromIndex)
          Finds the last instance of a String in this StringBuilder, starting at a given index.
 int length()
          Get the length of the String this StringBuilder would create.
private  boolean regionMatches(int toffset, String other)
          Predicate which determines if a substring of this matches another String starting at a specified offset for each String and continuing for a specified length.
 StringBuilder replace(int start, int end, String str)
          Replace characters between index start (inclusive) and end (exclusive) with str.
 StringBuilder reverse()
          Reverse the characters in this StringBuilder.
 void setCharAt(int index, char ch)
          Set the character at the specified index.
 void setLength(int newLength)
          Set the length of this StringBuilder.
 CharSequence subSequence(int beginIndex, int endIndex)
          Creates a substring of this StringBuilder, starting at a specified index and ending at one character before a specified index.
 String substring(int beginIndex)
          Creates a substring of this StringBuilder, starting at a specified index and ending at the end of this StringBuilder.
 String substring(int beginIndex, int endIndex)
          Creates a substring of this StringBuilder, starting at a specified index and ending at one character before a specified index.
 String toString()
          Convert this StringBuilder to a String.
 void trimToSize()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
For compatability with Sun's JDK

See Also:
Constant Field Values

count

int count
Index of next available character (and thus the size of the current string contents). Note that this has permissions set this way so that String can get the value.


value

char[] value
The buffer. Note that this has permissions set this way so that String can get the value.


DEFAULT_CAPACITY

private static final int DEFAULT_CAPACITY
The default capacity of a buffer.

See Also:
Constant Field Values
Constructor Detail

StringBuilder

public StringBuilder()
Create a new StringBuilder with default capacity 16.


StringBuilder

public StringBuilder(int capacity)
Create an empty StringBuilder with the specified initial capacity.


StringBuilder

public StringBuilder(String str)
Create a new StringBuilder with the characters in the specified String. Initial capacity will be the size of the String plus 16.


StringBuilder

public StringBuilder(CharSequence seq)
Create a new StringBuilder with the characters in the specified CharSequence. Initial capacity will be the length of the sequence plus 16; if the sequence reports a length less than or equal to 0, then the initial capacity will be 16.

Method Detail

length

public int length()
Get the length of the String this StringBuilder would create. Not to be confused with the capacity of the StringBuilder.

Specified by:
length in interface CharSequence

capacity

public int capacity()
Get the total number of characters this StringBuilder can support before it must be grown. Not to be confused with length.


ensureCapacity

public void ensureCapacity(int minimumCapacity)
Increase the capacity of this StringBuilder. This will ensure that an expensive growing operation will not occur until minimumCapacity is reached. The buffer is grown to the larger of minimumCapacity and capacity() * 2 + 2, if it is not already large enough.


setLength

public void setLength(int newLength)
Set the length of this StringBuilder. If the new length is greater than the current length, all the new characters are set to '\0'. If the new length is less than the current length, the first newLength characters of the old array will be preserved, and the remaining characters are truncated.


charAt

public char charAt(int index)
Get the character at the specified index.

Specified by:
charAt in interface CharSequence

getChars

public void getChars(int srcOffset,
                     int srcEnd,
                     char[] dst,
                     int dstOffset)
Get the specified array of characters. srcOffset - srcEnd characters will be copied into the array you pass in.


setCharAt

public void setCharAt(int index,
                      char ch)
Set the character at the specified index.


append

public StringBuilder append(Object obj)
Append the String value of the argument to this StringBuilder. Uses String.valueOf() to convert to String.


append

public StringBuilder append(String str)
Append the String to this StringBuilder. If str is null, the String "null" is appended.


append

public StringBuilder append(StringBuffer stringBuffer)
Append the StringBuilder value of the argument to this StringBuilder. This behaves the same as append((Object) stringBuffer), except it is more efficient.


append

public StringBuilder append(char[] data)
Append the char array to this StringBuilder. This is similar (but more efficient) than append(new String(data)), except in the case of null.


append

public StringBuilder append(char[] data,
                            int offset,
                            int count)
Append part of the char array to this StringBuilder. This is similar (but more efficient) than append(new String(data, offset, count)), except in the case of null.


append

public StringBuilder append(boolean bool)
Append the String value of the argument to this StringBuilder. Uses String.valueOf() to convert to String.


append

public StringBuilder append(char ch)
Append the char to this StringBuilder.


append

public StringBuilder append(CharSequence seq)
Append the characters in the CharSequence to this buffer.


append

public StringBuilder append(CharSequence seq,
                            int start,
                            int end)
Append some characters from the CharSequence to this buffer. If the argument is null, the four characters "null" are appended.


appendCodePoint

public StringBuilder appendCodePoint(int code)
Append the code point to this StringBuilder. This is like #append(char), but will append two characters if a supplementary code point is given.

Since:
1.5

append

public StringBuilder append(int inum)
Append the String value of the argument to this StringBuilder. Uses String.valueOf() to convert to String.


append

public StringBuilder append(long lnum)
Append the String value of the argument to this StringBuilder. Uses String.valueOf() to convert to String.


append

public StringBuilder append(float fnum)
Append the String value of the argument to this StringBuilder. Uses String.valueOf() to convert to String.


append

public StringBuilder append(double dnum)
Append the String value of the argument to this StringBuilder. Uses String.valueOf() to convert to String.


delete

public StringBuilder delete(int start,
                            int end)
Delete characters from this StringBuilder. delete(10, 12) will delete 10 and 11, but not 12. It is harmless for end to be larger than length().


deleteCharAt

public StringBuilder deleteCharAt(int index)
Delete a character from this StringBuilder.


replace

public StringBuilder replace(int start,
                             int end,
                             String str)
Replace characters between index start (inclusive) and end (exclusive) with str. If end is larger than the size of this StringBuilder, all characters after start are replaced.


substring

public String substring(int beginIndex)
Creates a substring of this StringBuilder, starting at a specified index and ending at the end of this StringBuilder.


subSequence

public CharSequence subSequence(int beginIndex,
                                int endIndex)
Creates a substring of this StringBuilder, starting at a specified index and ending at one character before a specified index. This is implemented the same as substring(beginIndex, endIndex), to satisfy the CharSequence interface.

Specified by:
subSequence in interface CharSequence

substring

public String substring(int beginIndex,
                        int endIndex)
Creates a substring of this StringBuilder, starting at a specified index and ending at one character before a specified index.


insert

public StringBuilder insert(int offset,
                            char[] str,
                            int str_offset,
                            int len)
Insert a subarray of the char[] argument into this StringBuilder.


insert

public StringBuilder insert(int offset,
                            Object obj)
Insert the String value of the argument into this StringBuilder. Uses String.valueOf() to convert to String.


insert

public StringBuilder insert(int offset,
                            String str)
Insert the String argument into this StringBuilder. If str is null, the String "null" is used instead.


insert

public StringBuilder insert(int offset,
                            CharSequence sequence)
Insert the CharSequence argument into this StringBuilder. If the sequence is null, the String "null" is used instead.


insert

public StringBuilder insert(int offset,
                            CharSequence sequence,
                            int start,
                            int end)
Insert a subsequence of the CharSequence argument into this StringBuilder. If the sequence is null, the String "null" is used instead.


insert

public StringBuilder insert(int offset,
                            char[] data)
Insert the char[] argument into this StringBuilder.


insert

public StringBuilder insert(int offset,
                            boolean bool)
Insert the String value of the argument into this StringBuilder. Uses String.valueOf() to convert to String.


insert

public StringBuilder insert(int offset,
                            char ch)
Insert the char argument into this StringBuilder.


insert

public StringBuilder insert(int offset,
                            int inum)
Insert the String value of the argument into this StringBuilder. Uses String.valueOf() to convert to String.


insert

public StringBuilder insert(int offset,
                            long lnum)
Insert the String value of the argument into this StringBuilder. Uses String.valueOf() to convert to String.


insert

public StringBuilder insert(int offset,
                            float fnum)
Insert the String value of the argument into this StringBuilder. Uses String.valueOf() to convert to String.


insert

public StringBuilder insert(int offset,
                            double dnum)
Insert the String value of the argument into this StringBuilder. Uses String.valueOf() to convert to String.


indexOf

public int indexOf(String str)
Finds the first instance of a substring in this StringBuilder.


indexOf

public int indexOf(String str,
                   int fromIndex)
Finds the first instance of a String in this StringBuilder, starting at a given index. If starting index is less than 0, the search starts at the beginning of this String. If the starting index is greater than the length of this String, or the substring is not found, -1 is returned.


lastIndexOf

public int lastIndexOf(String str)
Finds the last instance of a substring in this StringBuilder.


lastIndexOf

public int lastIndexOf(String str,
                       int fromIndex)
Finds the last instance of a String in this StringBuilder, starting at a given index. If starting index is greater than the maximum valid index, then the search begins at the end of this String. If the starting index is less than zero, or the substring is not found, -1 is returned.


reverse

public StringBuilder reverse()
Reverse the characters in this StringBuilder. The same sequence of characters exists, but in the reverse index ordering.


toString

public String toString()
Convert this StringBuilder to a String. The String is composed of the characters currently in this StringBuilder. Note that the result is a copy, and that future modifications to this buffer do not affect the String.

Specified by:
toString in interface CharSequence
Overrides:
toString in class Object

regionMatches

private boolean regionMatches(int toffset,
                              String other)
Predicate which determines if a substring of this matches another String starting at a specified offset for each String and continuing for a specified length. This is more efficient than creating a String to call indexOf on.


codePointAt

public int codePointAt(int index)
Get the code point at the specified index. This is like #charAt(int), but if the character is the start of a surrogate pair, and the following character completes the pair, then the corresponding supplementary code point is returned.

Since:
1.5

codePointBefore

public int codePointBefore(int index)
Get the code point before the specified index. This is like #codePointAt(int), but checks the characters at index-1 and index-2 to see if they form a supplementary code point.

Since:
1.5

codePointCount

public int codePointCount(int beginIndex,
                          int endIndex)
Returns the number of Unicode code points in the specified sub sequence. Surrogate pairs count as one code point.


trimToSize

public void trimToSize()