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 obj)
Overrides java. Appends the string representation of the |
| public StringBuilder | append(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 s)
Overrides java. Implements java. Appends the specified character sequence to this |
| public StringBuilder | append(CharSequence s, int start, int end)
Overrides java. Implements java. Appends a subsequence of the specified character sequence to this
|
| public StringBuilder | append(char[] str)
Overrides java. Appends the string representation of the |
| public StringBuilder | append(char[] str, int offset, int len)
Overrides java. Appends the string representation of a subarray of the
|
| public StringBuilder | append(boolean b)
Overrides java. Appends the string representation of the |
| public StringBuilder | append(char c)
Overrides java. Implements java. Appends the specified character to this |
| public StringBuilder | append(int i)
Overrides java. Appends the string representation of the |
| public StringBuilder | append(long lng)
Overrides java. Appends the string representation of the |
| public StringBuilder | append(float f)
Overrides java. Appends the string representation of the |
| public StringBuilder | append(double d)
Overrides java. Appends the string representation of the |
| public StringBuilder | appendCodePoint(int 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 start, int end)
Overrides java. Removes the characters in a substring of this sequence. |
| public StringBuilder | deleteCharAt(int index)
Overrides java. Removes the |
| public int | indexOf(String str)
Overrides java. Returns the index within this string of the first occurrence of the specified substring. |
| public int | indexOf(String str, int 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 index, char[] str, int offset, int len)
Overrides java. Inserts the string representation of a subarray of the |
| public StringBuilder | insert(int offset, Object obj)
Overrides java. Inserts the string representation of the |
| public StringBuilder | insert(int offset, String str)
Overrides java. Inserts the string into this character sequence. |
| public StringBuilder | insert(int offset, char[] str)
Overrides java. Inserts the string representation of the |
| public StringBuilder | insert(int dstOffset, CharSequence s)
Overrides java. Inserts the specified |
| public StringBuilder | insert(int dstOffset, CharSequence s, int start, int end)
Overrides java. Inserts a subsequence of the specified |
| public StringBuilder | insert(int offset, boolean b)
Overrides java. Inserts the string representation of the |
| public StringBuilder | insert(int offset, char c)
Overrides java. Inserts the string representation of the |
| public StringBuilder | insert(int offset, int i)
Overrides java. Inserts the string representation of the second |
| public StringBuilder | insert(int offset, long l)
Overrides java. Inserts the string representation of the |
| public StringBuilder | insert(int offset, float f)
Overrides java. Inserts the string representation of the |
| public StringBuilder | insert(int offset, double d)
Overrides java. Inserts the string representation of the |
| public int | lastIndexOf(String str)
Overrides java. Returns the index within this string of the last occurrence of the specified substring. |
| public int | lastIndexOf(String str, int 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 codePoint, int count)
Overrides java. Repeats |
| public StringBuilder | repeat(CharSequence cs, int count)
Overrides java. Appends |
| public StringBuilder | replace(int start, int end, String 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 |
| serialVersionUID | back to summary |
|---|---|
| pack-priv static final long serialVersionUID use serialVersionUID for interoperability | |
| StringBuilder | back to summary |
|---|---|
| public StringBuilder() Constructs a string builder with no characters in it and an initial capacity of 16 characters.
| |
| StringBuilder | back to summary |
|---|---|
| public StringBuilder(int capacity) Constructs a string builder with no characters in it and an
initial capacity specified by the
| |
| StringBuilder | back to summary |
|---|---|
| public StringBuilder(String str) Constructs a string builder initialized to the contents of the
specified string. The initial capacity of the string builder is
| |
| StringBuilder | back to summary |
|---|---|
| public StringBuilder(CharSequence seq) Constructs a string builder that contains the same characters
as the specified
| |
| append | back to summary |
|---|---|
| public StringBuilder append(Object obj) Overrides java. Doc from java. Appends the string representation of the
The overall effect is exactly as if the argument were converted
to a string by the method
| |
| append | back to summary |
|---|---|
| public StringBuilder append(String str) Overrides java. Doc from java. Appends the specified string to this character sequence.
The characters of the
Let n be the length of this character sequence just prior to
execution of the
| |
| append | back to summary |
|---|---|
| public StringBuilder append(StringBuffer sb) Overrides java. Appends the specified
The characters of the
Let n be the length of this character sequence just prior to
execution of the
| |
| append | back to summary |
|---|---|
| public StringBuilder append(CharSequence s) Overrides java. Implements java. Doc from java. Appends the specified character sequence to this Depending on which class implements the character sequence
The contents of this
| |
| append | back to summary |
|---|---|
| public StringBuilder append(CharSequence s, int start, int end) Overrides java. Implements java. Doc from java. Appends a subsequence of the specified character sequence to this
An invocation of this method of the form
out.append(csq.subSequence(start, end))
The contents of this
| |
| append | back to summary |
|---|---|
| public StringBuilder append(char[] str) Overrides java. Doc from java. Appends the string representation of the The characters of the array argument are appended, in order, to the contents of this sequence. The length of this sequence increases by the length of the argument.
The overall effect is exactly as if the argument were converted
to a string by the method
| |
| append | back to summary |
|---|---|
| public StringBuilder append(char[] str, int offset, int len) Overrides java. Doc from java. Appends the string representation of a subarray of the
Characters of the
The overall effect is exactly as if the arguments were converted
to a string by the method
| |
| append | back to summary |
|---|---|
| public StringBuilder append(boolean b) Overrides java. Doc from java. Appends the string representation of the
The overall effect is exactly as if the argument were converted
to a string by the method
| |
| append | back to summary |
|---|---|
| public StringBuilder append(char c) Overrides java. Implements java. Doc from java. Appends the specified character to this
| |
| append | back to summary |
|---|---|
| public StringBuilder append(int i) Overrides java. Doc from java. Appends the string representation of the
The overall effect is exactly as if the argument were converted
to a string by the method
| |
| append | back to summary |
|---|---|
| public StringBuilder append(long lng) Overrides java. Doc from java. Appends the string representation of the
The overall effect is exactly as if the argument were converted
to a string by the method
| |
| append | back to summary |
|---|---|
| public StringBuilder append(float f) Overrides java. Doc from java. Appends the string representation of the
The overall effect is exactly as if the argument were converted
to a string by the method
| |
| append | back to summary |
|---|---|
| public StringBuilder append(double d) Overrides java. Doc from java. Appends the string representation of the
The overall effect is exactly as if the argument were converted
to a string by the method
| |
| appendCodePoint | back to summary |
|---|---|
| public StringBuilder appendCodePoint(int codePoint) Overrides java. Doc from java. Appends the string representation of the The argument is appended to the contents of this sequence.
The length of this sequence increases by
The overall effect is exactly as if the argument were
converted to a
| |
| compareTo | back to summary |
|---|---|
| public int compareTo(StringBuilder another) Implements java. Compares two
For finer-grained, locale-sensitive String comparison, refer to
| |
| delete | back to summary |
|---|---|
| public StringBuilder delete(int start, int end) Overrides java. Doc from java. Removes the characters in a substring of this sequence.
The substring begins at the specified
| |
| deleteCharAt | back to summary |
|---|---|
| public StringBuilder deleteCharAt(int index) Overrides java. Doc from java. Removes the Note If the character at the given index is a supplementary
character, this method does not remove the entire character. If
correct handling of supplementary characters is required,
determine the number of
| |
| indexOf | back to summary |
|---|---|
| public int indexOf(String str) Overrides java. Doc from java. Returns the index within this string of the first occurrence of the specified substring. The returned index is the smallest value
If no such value of k exists, then -1 is returned.
| |
| indexOf | back to summary |
|---|---|
| public int indexOf(String str, int fromIndex) Overrides java. Doc from java. Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The returned index is the smallest value
If no such value of k exists, then -1 is returned.
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int index, char[] str, int offset, int len) Overrides java. Doc from java. Inserts the string representation of a subarray of the
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, Object obj) Overrides java. Doc from java. Inserts the string representation of the
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, String str) Overrides java. Doc from java. Inserts the string into this character sequence.
The characters of the The character at index k in the new character sequence is equal to:
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, char[] str) Overrides java. Doc from java. Inserts the string representation of the
The characters of the array argument are inserted into the
contents of this sequence at the position indicated by
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int dstOffset, CharSequence s) Overrides java. Doc from java. Inserts the specified
The characters of the
The result of this method is exactly the same as if it were an
invocation of this object's
The contents are unspecified if the If
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int dstOffset, CharSequence s, int start, int end) Overrides java. Doc from java. Inserts a subsequence of the specified
The subsequence of the argument The character at index k in this sequence becomes equal to:
The The start argument must be non-negative, and not greater than
The end argument must be greater than or equal to
If
The contents are unspecified if the
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, boolean b) Overrides java. Doc from java. Inserts the string representation of the
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, char c) Overrides java. Doc from java. Inserts the string representation of the
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, int i) Overrides java. Doc from java. Inserts the string representation of the second
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, long l) Overrides java. Doc from java. Inserts the string representation of the
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, float f) Overrides java. Doc from java. Inserts the string representation of the
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| insert | back to summary |
|---|---|
| public StringBuilder insert(int offset, double d) Overrides java. Doc from java. Inserts the string representation of the
The overall effect is exactly as if the second argument were
converted to a string by the method
The
| |
| lastIndexOf | back to summary |
|---|---|
| public int lastIndexOf(String str) Overrides java. Doc from java. Returns the index within this string of the last occurrence of the
specified substring. The last occurrence of the empty string "" is
considered to occur at the index value The returned index is the largest value
If no such value of k exists, then -1 is returned.
| |
| lastIndexOf | back to summary |
|---|---|
| public int lastIndexOf(String str, int fromIndex) Overrides java. Doc from java. Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. The returned index is the largest value
If no such value of k exists, then -1 is returned.
| |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException readObject is called to restore the state of the StringBuilder from a stream.
| |
| repeat | back to summary |
|---|---|
| public StringBuilder repeat(int codePoint, int count) Overrides java. Doc from java. Repeats
The length of this sequence increases by
It is usual to use // insert 10 asterisks into the buffer sb.repeat('*', 10);
| |
| repeat | back to summary |
|---|---|
| public StringBuilder repeat(CharSequence cs, int count) Overrides java. Doc from java. Appends
The length of this sequence increases by
If
The contents are unspecified if the
| |
| replace | back to summary |
|---|---|
| public StringBuilder replace(int start, int end, String str) Overrides java. Doc from java. Replaces the characters in a substring of this sequence
with characters in the specified
| |
| reverse | back to summary |
|---|---|
| public StringBuilder reverse() Overrides java. Doc from java. Causes this character sequence to be replaced by the reverse of
the sequence. If there are any surrogate pairs included in the
sequence, these are treated as single characters for the
reverse operation. Thus, the order of the high-low surrogates
is never reversed.
Let n be the character length of this character sequence
(not the length in Note that the reverse operation may result in producing surrogate pairs that were unpaired low-surrogates and high-surrogates before the operation. For example, reversing "\uDC00\uD800" produces "\uD800\uDC00" which is a valid surrogate pair.
| |
| toString | back to summary |
|---|---|
| public String toString() Implements abstract java. Implements java. Doc from java. Returns a string containing the characters in this sequence in the same order as this sequence. The length of the string will be the length of this sequence.
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(ObjectOutputStream s) throws IOException Save the state of the
| |