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. Object argument.
|
public StringBuilder | append(String
a string. str)Overrides java. |
public StringBuilder | Returns: a reference to this object.the sb)StringBuffer to append.Overrides java. StringBuffer to this sequence.
|
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. Appendable .
|
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. Appendable .
|
public StringBuilder | append(char[]
the characters to be appended. str)Overrides java. char array
argument to this sequence.
|
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. char array argument to this sequence.
|
public StringBuilder | append(boolean
a b)boolean .Overrides java. boolean
argument to the sequence.
|
public StringBuilder | append(char
The character to append c)Overrides java. Implements java. Appendable .
|
public StringBuilder | append(int
an i)int .Overrides java. int
argument to this sequence.
|
public StringBuilder | append(long
a lng)long .Overrides java. long
argument to this sequence.
|
public StringBuilder | append(float
a f)float .Overrides java. float
argument to this sequence.
|
public StringBuilder | append(double
a d)double .Overrides java. double
argument to this sequence.
|
public StringBuilder | appendCodePoint(int
a Unicode code point codePoint)Overrides java. codePoint
argument to this sequence.
|
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. StringBuilder instances lexicographically.
|
public StringBuilder | delete(int
The beginning index, inclusive. start, int The ending index, exclusive. end)Overrides java. |
public StringBuilder | deleteCharAt(int
Index of index)char to removeOverrides java. char at the specified position in this
sequence.
|
public int | indexOf(String
the substring to search for. str)Overrides java. |
public int | indexOf(String
the substring to search for. str, int the index from which to start the search. fromIndex)Overrides java. |
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. str
array argument into this sequence.
|
public StringBuilder | insert(int
the offset. offset, Object an obj)Object .Overrides java. Object
argument into this character sequence.
|
public StringBuilder | insert(int
the offset. offset, String a string. str)Overrides java. |
public StringBuilder | insert(int
the offset. offset, char[] a character array. str)Overrides java. char array
argument into this sequence.
|
public StringBuilder | insert(int
the offset. dstOffset, CharSequence the sequence to be inserted s)Overrides java. CharSequence into this sequence.
|
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. CharSequence into
this sequence.
|
public StringBuilder | insert(int
the offset. offset, boolean a b)boolean .Overrides java. boolean
argument into this sequence.
|
public StringBuilder | insert(int
the offset. offset, char a c)char .Overrides java. char
argument into this sequence.
|
public StringBuilder | insert(int
the offset. offset, int an i)int .Overrides java. int
argument into this sequence.
|
public StringBuilder | insert(int
the offset. offset, long a l)long .Overrides java. long
argument into this sequence.
|
public StringBuilder | insert(int
the offset. offset, float a f)float .Overrides java. float
argument into this sequence.
|
public StringBuilder | insert(int
the offset. offset, double a d)double .Overrides java. double
argument into this sequence.
|
public int | lastIndexOf(String
the substring to search for. str)Overrides java. |
public int | lastIndexOf(String
the substring to search for. str, int the index to start the search from. fromIndex)Overrides java. |
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. count copies of the string representation of the
codePoint argument to this sequence.
|
public StringBuilder | repeat(CharSequence
a cs, int CharSequence number of times to copy count)Overrides java. count copies of the specified CharSequence cs
to this sequence.
|
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. String .
|
public StringBuilder | reverse()
Overrides java. |
public String | toString()
Implements abstract java. Implements java. |
private void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to which data is writtenSave the state of the |