Class: java.lang.StringBuilder
- public final class StringBuilder
- extends AbstractStringBuilder
- implements Serializable, CharSequence
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 StringBuffer be used.
Methods
-
StringBuildertop
public StringBuilder()Constructs a string builder with no characters in it and an initial capacity of 16 characters. -
StringBuildertop
public StringBuilder(int capacity)Constructs a string builder with no characters in it and an initial capacity specified by thecapacityargument. -
StringBuildertop
public StringBuilder(CharSequence seq)Constructs a string builder that contains the same characters as the specifiedCharSequence. The initial capacity of the string builder is16plus the length of theCharSequenceargument. -
StringBuildertop
public StringBuilder(String str)Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is16plus the length of the string argument. -
appendtop
public StringBuilder append(char c)Appends the specified character to this Appendable.- Specified by:
- append from Appendable
-
appendtop
public StringBuilder append(double d) -
appendtop
public StringBuilder append(float f) -
appendtop
public StringBuilder append(int i) -
appendtop
public StringBuilder append(long lng) -
appendtop
public StringBuilder append(CharSequence s)Appends the specified character sequence to this Appendable.Depending on which class implements the character sequence csq, the entire sequence may not be appended. For instance, if csq is a java.nio.CharBuffer then the subsequence to append is defined by the buffer's position and limit.
- Specified by:
- append from Appendable
-
appendtop
public StringBuilder append(CharSequence s, int start, int end)Appends a subsequence of the specified character sequence to this Appendable.An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation
out.append(csq.subSequence(start, end))- Specified by:
- append from Appendable
-
appendtop
public StringBuilder append(Object obj) -
appendtop
public StringBuilder append(String str) -
appendtop
public StringBuilder append(StringBuffer sb)Appends the specified StringBuffer to this sequence.The characters of the StringBuffer argument are appended, in order, to this sequence, increasing the length of this sequence by the length of the argument. If sb is null, then the four characters "null" are appended to this sequence.
Let n be the length of this character sequence just prior to execution of the append method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument
sb. -
appendtop
public StringBuilder append(boolean b) -
appendtop
public StringBuilder append(char[] str) -
appendtop
public StringBuilder append(char[] str, int offset, int len) -
appendCodePointtop
public StringBuilder appendCodePoint(int codePoint) -
deletetop
public StringBuilder delete(int start, int end) -
deleteCharAttop
public StringBuilder deleteCharAt(int index) -
indexOftop
public int indexOf(String str) -
indexOftop
public int indexOf(String str, int fromIndex) -
inserttop
public StringBuilder insert(int offset, char c) -
inserttop
public StringBuilder insert(int offset, double d) -
inserttop
public StringBuilder insert(int offset, float f) -
inserttop
public StringBuilder insert(int offset, int i) -
inserttop
public StringBuilder insert(int offset, long l) -
inserttop
public StringBuilder insert(int dstOffset, CharSequence s) -
inserttop
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end) -
inserttop
public StringBuilder insert(int offset, Object obj) -
inserttop
public StringBuilder insert(int offset, String str) -
inserttop
public StringBuilder insert(int offset, boolean b) -
inserttop
public StringBuilder insert(int offset, char[] str) -
inserttop
public StringBuilder insert(int index, char[] str, int offset, int len) -
lastIndexOftop
public int lastIndexOf(String str) -
lastIndexOftop
public int lastIndexOf(String str, int fromIndex) -
replacetop
public StringBuilder replace(int start, int end, String str) -
reversetop
public StringBuilder reverse() -
toStringtop
public String toString()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.- Specified by:
- toString from CharSequence
