StringBuilder

public final class StringBuilder extends Object
implements Appendable CharSequence Serializable CharSequence

A mutable sequence of characters. This class provides an API compatible with 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.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Public Constructor Summary

StringBuilder()
Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(int capacity)
Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
StringBuilder(String str)
Constructs a string builder initialized to the contents of the specified string.
StringBuilder(CharSequence seq)
Constructs a string builder that contains the same characters as the specified CharSequence.

Public Method Summary

StringBuilder
append(boolean b)
StringBuilder
append(long lng)
StringBuilder
append(char c)
Appends the specified character to this Appendable.
StringBuilder
append(Object obj)
StringBuilder
append(char[] str, int offset, int len)
StringBuilder
append(double d)
StringBuilder
append(char[] str)
StringBuilder
append(String str)
StringBuilder
append(StringBuffer sb)
Appends the specified StringBuffer to this sequence.
StringBuilder
append(float f)
StringBuilder
append(int i)
StringBuilder
append(CharSequence s, int start, int end)
Appends a subsequence of the specified character sequence to this Appendable.
StringBuilder
append(CharSequence s)
Appends the specified character sequence to this Appendable.
StringBuilder
appendCodePoint(int codePoint)
int
char
charAt(int i)
Returns the char value at the specified index.
int
codePointAt(int i)
int
int
codePointCount(int i, int j)
StringBuilder
delete(int start, int end)
StringBuilder
deleteCharAt(int index)
void
void
getChars(int i, int j, char[] buf, int k)
int
int
indexOf(String s, int i)
StringBuilder
insert(int offset, char[] str)
StringBuilder
insert(int offset, float f)
StringBuilder
insert(int dstOffset, CharSequence s)
StringBuilder
insert(int offset, char c)
StringBuilder
insert(int offset, long l)
StringBuilder
insert(int index, char[] str, int offset, int len)
StringBuilder
insert(int offset, int i)
StringBuilder
insert(int offset, String str)
StringBuilder
insert(int offset, double d)
StringBuilder
insert(int dstOffset, CharSequence s, int start, int end)
StringBuilder
insert(int offset, Object obj)
StringBuilder
insert(int offset, boolean b)
int
lastIndexOf(String s, int i)
int
int
length()
Returns the length of this character sequence.
int
offsetByCodePoints(int i, int j)
StringBuilder
replace(int start, int end, String str)
StringBuilder
void
setCharAt(int i, char c)
void
setLength(int i)
CharSequence
subSequence(int i, int j)
Returns a new CharSequence that is a subsequence of this sequence.
String
substring(int i, int j)
String
substring(int i)
String
toString()
Returns a string containing a concise, human-readable description of this object.
void

Inherited Method Summary

Public Constructors

public StringBuilder ()

Constructs a string builder with no characters in it and an initial capacity of 16 characters.

public StringBuilder (int capacity)

Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

Parameters
capacity the initial capacity.
Throws
NegativeArraySizeException if the capacity argument is less than 0.

public StringBuilder (String str)

Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument.

Parameters
str the initial contents of the buffer.

public StringBuilder (CharSequence seq)

Constructs a string builder that contains the same characters as the specified CharSequence. The initial capacity of the string builder is 16 plus the length of the CharSequence argument.

Parameters
seq the sequence to copy.

Public Methods

public StringBuilder append (boolean b)

Parameters
b

public StringBuilder append (long lng)

Parameters
lng

public StringBuilder append (char c)

Appends the specified character to this Appendable.

Parameters
c The character to append
Returns
  • A reference to this Appendable

public StringBuilder append (Object obj)

Parameters
obj

public StringBuilder append (char[] str, int offset, int len)

Parameters
str
offset
len

public StringBuilder append (double d)

Parameters
d

public StringBuilder append (char[] str)

Parameters
str

public StringBuilder append (String str)

Parameters
str

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.

Parameters
sb the StringBuffer to append.
Returns
  • a reference to this object.

public StringBuilder append (float f)

Parameters
f

public StringBuilder append (int i)

Parameters
i

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)) 

Parameters
s The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start The index of the first character in the subsequence
end The index of the character following the last character in the subsequence
Returns
  • A reference to this Appendable

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 CharBuffer then the subsequence to append is defined by the buffer's position and limit.

Parameters
s The character sequence to append. If csq is null, then the four characters "null" are appended to this Appendable.
Returns
  • A reference to this Appendable

public StringBuilder appendCodePoint (int codePoint)

Parameters
codePoint

public int capacity ()

public char charAt (int i)

Returns the char value at the specified index. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

Parameters
i the index of the char value to be returned
Returns
  • the specified char value

public int codePointAt (int i)

Parameters
i

public int codePointBefore (int i)

Parameters
i

public int codePointCount (int i, int j)

Parameters
i
j

public StringBuilder delete (int start, int end)

Parameters
start
end

public StringBuilder deleteCharAt (int index)

Parameters
index

public void ensureCapacity (int i)

Parameters
i

public void getChars (int i, int j, char[] buf, int k)

Parameters
i
j
buf
k

public int indexOf (String s)

Parameters
s

public int indexOf (String s, int i)

Parameters
s
i

public StringBuilder insert (int offset, char[] str)

Parameters
offset
str

public StringBuilder insert (int offset, float f)

Parameters
offset
f

public StringBuilder insert (int dstOffset, CharSequence s)

Parameters
dstOffset
s

public StringBuilder insert (int offset, char c)

Parameters
offset
c

public StringBuilder insert (int offset, long l)

Parameters
offset
l

public StringBuilder insert (int index, char[] str, int offset, int len)

Parameters
index
str
offset
len

public StringBuilder insert (int offset, int i)

Parameters
offset
i

public StringBuilder insert (int offset, String str)

Parameters
offset
str

public StringBuilder insert (int offset, double d)

Parameters
offset
d

public StringBuilder insert (int dstOffset, CharSequence s, int start, int end)

Parameters
dstOffset
s
start
end

public StringBuilder insert (int offset, Object obj)

Parameters
offset
obj

public StringBuilder insert (int offset, boolean b)

Parameters
offset
b

public int lastIndexOf (String s, int i)

Parameters
s
i

public int lastIndexOf (String s)

Parameters
s

public int length ()

Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.

Returns
  • the number of chars in this sequence

public int offsetByCodePoints (int i, int j)

Parameters
i
j

public StringBuilder replace (int start, int end, String str)

Parameters
start
end
str

public StringBuilder reverse ()

public void setCharAt (int i, char c)

Parameters
i
c

public void setLength (int i)

Parameters
i

public CharSequence subSequence (int i, int j)

Returns a new CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.

Parameters
i the start index, inclusive
j the end index, exclusive
Returns
  • the specified subsequence

public String substring (int i, int j)

Parameters
i
j

public String substring (int i)

Parameters
i

public String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.

public void trimToSize ()