StringBuffer
, but with no guarantee of synchronization.
This class is designed for use as a drop-in replacement for
StringBuffer
in places where the string buffer was being
used by a single thread (as is generally the case). Where possible,
it is recommended that this class be used in preference to
StringBuffer
as it will be faster under most implementations.
The principal operations on a StringBuilder
are the
append
and insert
methods, which are
overloaded so as to accept data of any type. Each effectively
converts a given datum to a string and then appends or inserts the
characters of that string to the string builder. The
append
method always adds these characters at the end
of the builder; the insert
method adds the characters at
a specified point.
For example, if z
refers to a string builder object
whose current contents are "start
", then
the method call z.append("le")
would cause the string
builder to contain "startle
", whereas
z.insert(4, "le")
would alter the string builder to
contain "starlet
".
In general, if sb refers to an instance of a StringBuilder
,
then sb.append(x)
has the same effect as
sb.insert(sb.length(), x)
.
Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.
Instances of StringBuilder
are not safe for
use by multiple threads. If such synchronization is required then it is
recommended that java.
be used.
Unless otherwise noted, passing a null
argument to a constructor
or method in this class will cause a NullPointerException
to be
thrown.
API Note
StringBuilder
implements Comparable
but does not override
equals
. Thus, the natural ordering of StringBuilder
is inconsistent with equals. Care should be exercised if StringBuilder
objects are used as keys in a SortedMap
or elements in a SortedSet
.
See Comparable
, SortedMap
, or
SortedSet
for more information.
java.lang.StringBuffer
, java.lang.String
Modifier and Type | Field and Description |
---|---|
pack-priv static final long | serialVersionUID
use serialVersionUID for interoperability |
Access | Constructor and Description |
---|---|
public | StringBuilder()
Constructs a string builder with no characters in it and an initial capacity of 16 characters. |
public | StringBuilder(int
the initial capacity. capacity)Constructs a string builder with no characters in it and an
initial capacity specified by the |
public | StringBuilder(String
the initial contents of the buffer. str)Constructs a string builder initialized to the contents of the specified string. |
public | StringBuilder(CharSequence
the sequence to copy. seq)Constructs a string builder that contains the same characters
as the specified |
Modifier and Type | Method and Description |
---|---|
public StringBuilder | append(Object
an obj)Object .Overrides java. Appends the string representation of the |
public StringBuilder | append(String
a string. str)Overrides java. Appends the specified string to this character sequence. |
public StringBuilder | Returns: a reference to this object.the sb)StringBuffer to append.Overrides java. Appends the specified |
public StringBuilder | append(CharSequence
The character sequence to append. If s)csq is
null , then the four characters "null" are
appended to this Appendable.Overrides java. Implements java. Appends the specified character sequence to this |
public StringBuilder | append(CharSequence
The character sequence from which a subsequence will be
appended. If s, int csq is null , then characters
will be appended as if csq contained the four
characters "null" .The index of the first character in the subsequence start, int The index of the character following the last character in the
subsequence end)Overrides java. Implements java. Appends a subsequence of the specified character sequence to this
|
public StringBuilder | append(char[]
the characters to be appended. str)Overrides java. Appends the string representation of the |
public StringBuilder | append(char[]
the characters to be appended. str, int the index of the first offset, int char to append.the number of len)char s to append.Overrides java. Appends the string representation of a subarray of the
|
public StringBuilder | append(boolean
a b)boolean .Overrides java. Appends the string representation of the |
public StringBuilder | append(char
The character to append c)Overrides java. Implements java. Appends the specified character to this |
public StringBuilder | append(int
an i)int .Overrides java. Appends the string representation of the |
public StringBuilder | append(long
a lng)long .Overrides java. Appends the string representation of the |
public StringBuilder | append(float
a f)float .Overrides java. Appends the string representation of the |
public StringBuilder | append(double
a d)double .Overrides java. Appends the string representation of the |
public StringBuilder | appendCodePoint(int
a Unicode code point codePoint)Overrides java. Appends the string representation of the |
public int | Returns: the value0 if this StringBuilder contains the same
character sequence as that of the argument StringBuilder ; a negative integer
if this StringBuilder is lexicographically less than the
StringBuilder argument; or a positive integer if this StringBuilder
is lexicographically greater than the StringBuilder argument.the another)StringBuilder to be compared withImplements java. Compares two |
public StringBuilder | delete(int
The beginning index, inclusive. start, int The ending index, exclusive. end)Overrides java. Removes the characters in a substring of this sequence. |
public StringBuilder | deleteCharAt(int
Index of index)char to removeOverrides java. Removes the |
public int | indexOf(String
the substring to search for. str)Overrides java. Returns the index within this string of the first occurrence of the specified substring. |
public int | indexOf(String
the substring to search for. str, int the index from which to start the search. fromIndex)Overrides java. Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. |
public StringBuilder | insert(int
position at which to insert subarray. index, char[] A str, int char array.the index of the first offset, int char in subarray to
be inserted.the number of len)char s in the subarray to
be inserted.Overrides java. Inserts the string representation of a subarray of the |
public StringBuilder | insert(int
the offset. offset, Object an obj)Object .Overrides java. Inserts the string representation of the |
public StringBuilder | insert(int
the offset. offset, String a string. str)Overrides java. Inserts the string into this character sequence. |
public StringBuilder | insert(int
the offset. offset, char[] a character array. str)Overrides java. Inserts the string representation of the |
public StringBuilder | insert(int
the offset. dstOffset, CharSequence the sequence to be inserted s)Overrides java. Inserts the specified |
public StringBuilder | insert(int
the offset in this sequence. dstOffset, CharSequence the sequence to be inserted. s, int the starting index of the subsequence to be inserted. start, int the end index of the subsequence to be inserted. end)Overrides java. Inserts a subsequence of the specified |
public StringBuilder | insert(int
the offset. offset, boolean a b)boolean .Overrides java. Inserts the string representation of the |
public StringBuilder | insert(int
the offset. offset, char a c)char .Overrides java. Inserts the string representation of the |
public StringBuilder | insert(int
the offset. offset, int an i)int .Overrides java. Inserts the string representation of the second |
public StringBuilder | insert(int
the offset. offset, long a l)long .Overrides java. Inserts the string representation of the |
public StringBuilder | insert(int
the offset. offset, float a f)float .Overrides java. Inserts the string representation of the |
public StringBuilder | insert(int
the offset. offset, double a d)double .Overrides java. Inserts the string representation of the |
public int | lastIndexOf(String
the substring to search for. str)Overrides java. Returns the index within this string of the last occurrence of the specified substring. |
public int | lastIndexOf(String
the substring to search for. str, int the index to start the search from. fromIndex)Overrides java. Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. |
private void | readObject(ObjectInputStream
the s)ObjectInputStream from which data is readreadObject is called to restore the state of the StringBuilder from a stream. |
public StringBuilder | repeat(int
code point to append codePoint, int number of times to copy count)Overrides java. Repeats |
public StringBuilder | repeat(CharSequence
a cs, int CharSequence number of times to copy count)Overrides java. Appends |
public StringBuilder | replace(int
The beginning index, inclusive. start, int The ending index, exclusive. end, String String that will replace previous contents. str)Overrides java. Replaces the characters in a substring of this sequence
with characters in the specified |
public StringBuilder | reverse()
Overrides java. Causes this character sequence to be replaced by the reverse of the sequence. |
public String | toString()
Implements abstract java. Implements java. Returns a string containing the characters in this sequence in the same order as this sequence. |
private void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to which data is writtenSave the state of the |