StringBuilder - JRE Emulation | JRE Emulation
public final class

StringBuilder

extends Object
implements Appendable CharSequence Serializable
java.lang.Object
   ↳ java.lang.StringBuilder

Class Overview

A modifiable sequence of characters for use in creating strings. This class is intended as a direct replacement of StringBuffer for non-concurrent use; unlike StringBuffer this class is not synchronized.

For particularly complex string-building needs, consider Formatter.

The majority of the modification methods on this class return this so that method calls can be chained together. For example: new StringBuilder("a").append("b").append("c").toString().

Summary

Public Constructors
StringBuilder()
Constructs an instance with an initial capacity of 16.
StringBuilder(int capacity)
Constructs an instance with the specified capacity.
StringBuilder(CharSequence seq)
Constructs an instance that's initialized with the contents of the specified CharSequence.
StringBuilder(String str)
Constructs an instance that's initialized with the contents of the specified String.
Public Methods
StringBuilder append(boolean b)
Appends the string representation of the specified boolean value.
StringBuilder append(long l)
Appends the string representation of the specified long value.
StringBuilder append(char c)
Appends the string representation of the specified char value.
StringBuilder append(Object obj)
Appends the string representation of the specified Object.
StringBuilder append(char[] str, int offset, int len)
Appends the string representation of the specified subset of the char[].
StringBuilder append(double d)
Appends the string representation of the specified double value.
StringBuilder append(char[] chars)
Appends the string representation of the specified char[].
StringBuilder append(String str)
Appends the contents of the specified string.
StringBuilder append(StringBuffer sb)
Appends the contents of the specified StringBuffer.
StringBuilder append(float f)
Appends the string representation of the specified float value.
StringBuilder append(int i)
Appends the string representation of the specified int value.
StringBuilder append(CharSequence csq, int start, int end)
Appends the string representation of the specified subsequence of the CharSequence.
StringBuilder append(CharSequence csq)
Appends the string representation of the specified CharSequence.
StringBuilder appendCodePoint(int codePoint)
Appends the encoded Unicode code point.
int capacity()
char charAt(int i)
Returns the char value at the specified index.
int codePointAt(int i)
int codePointBefore(int i)
int codePointCount(int i, int j)
StringBuilder delete(int start, int end)
Deletes a sequence of characters specified by start and end.
StringBuilder deleteCharAt(int index)
Deletes the character at the specified index.
void ensureCapacity(int i)
void getChars(int i, int j, char[] buf, int k)
int indexOf(String s)
int indexOf(String s, int i)
StringBuilder insert(int offset, char[] ch)
Inserts the string representation of the specified char[] at the specified offset.
StringBuilder insert(int offset, float f)
Inserts the string representation of the specified float value at the specified offset.
StringBuilder insert(int offset, CharSequence s)
Inserts the string representation of the specified CharSequence at the specified offset.
StringBuilder insert(int offset, char c)
Inserts the string representation of the specified char value at the specified offset.
StringBuilder insert(int offset, long l)
Inserts the string representation of the specified long value at the specified offset.
StringBuilder insert(int offset, char[] str, int strOffset, int strLen)
Inserts the string representation of the specified subsequence of the char[] at the specified offset.
StringBuilder insert(int offset, int i)
Inserts the string representation of the specified int value at the specified offset.
StringBuilder insert(int offset, String str)
Inserts the specified string at the specified offset.
StringBuilder insert(int offset, double d)
Inserts the string representation of the specified double value at the specified offset.
StringBuilder insert(int offset, CharSequence s, int start, int end)
Inserts the string representation of the specified subsequence of the CharSequence at the specified offset.
StringBuilder insert(int offset, Object obj)
Inserts the string representation of the specified Object at the specified offset.
StringBuilder insert(int offset, boolean b)
Inserts the string representation of the specified boolean value at the specified offset.
int lastIndexOf(String s, int i)
int lastIndexOf(String s)
int length()
Returns the length of this character sequence.
int offsetByCodePoints(int i, int j)
StringBuilder replace(int start, int end, String string)
Replaces the specified subsequence in this builder with the specified string.
StringBuilder reverse()
Reverses the order of characters in this builder.
void setCharAt(int i, char c)
void setLength(int i)
CharSequence subSequence(int start, int end)
Returns a new CharSequence that is a subsequence of this sequence.
String substring(int i, int j)
String substring(int i)
String toString()
Returns the contents of this builder.
void trimToSize()
Inherited Methods
[Expand]
From class java.lang.Object
From interface java.lang.Appendable
From interface java.lang.CharSequence

Public Constructors

public StringBuilder ()

Constructs an instance with an initial capacity of 16.

See Also

public StringBuilder (int capacity)

Constructs an instance with the specified capacity.

Parameters
capacity the initial capacity to use.
Throws
NegativeArraySizeException if the specified capacity is negative.
See Also

public StringBuilder (CharSequence seq)

Constructs an instance that's initialized with the contents of the specified CharSequence. The capacity of the new builder will be the length of the CharSequence plus 16.

Parameters
seq the CharSequence to copy into the builder.
Throws
NullPointerException if seq is null.

public StringBuilder (String str)

Constructs an instance that's initialized with the contents of the specified String. The capacity of the new builder will be the length of the String plus 16.

Parameters
str the String to copy into the builder.
Throws
NullPointerException if str is null.

Public Methods

public StringBuilder append (boolean b)

Appends the string representation of the specified boolean value. The boolean value is converted to a String according to the rule defined by valueOf(boolean).

Parameters
b the boolean value to append.
Returns
  • this builder.
See Also

public StringBuilder append (long l)

Appends the string representation of the specified long value. The long value is converted to a string according to the rule defined by valueOf(long).

Parameters
l the long value.
Returns
  • this builder.
See Also

public StringBuilder append (char c)

Appends the string representation of the specified char value. The char value is converted to a string according to the rule defined by valueOf(char).

Parameters
c the char value to append.
Returns
  • this builder.
See Also

public StringBuilder append (Object obj)

Appends the string representation of the specified Object. The Object value is converted to a string according to the rule defined by valueOf(Object).

Parameters
obj the Object to append.
Returns
  • this builder.
See Also

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

Appends the string representation of the specified subset of the char[]. The char[] value is converted to a String according to the rule defined by valueOf(char[], int, int).

Parameters
str the char[] to append.
offset the inclusive offset index.
len the number of characters.
Returns
  • this builder.
Throws
ArrayIndexOutOfBoundsException if offset and len do not specify a valid subsequence.

public StringBuilder append (double d)

Appends the string representation of the specified double value. The double value is converted to a string according to the rule defined by valueOf(double).

Parameters
d the double value to append.
Returns
  • this builder.
See Also

public StringBuilder append (char[] chars)

Appends the string representation of the specified char[]. The char[] is converted to a string according to the rule defined by valueOf(char[]).

Parameters
chars the char[] to append..
Returns
  • this builder.
See Also

public StringBuilder append (String str)

Appends the contents of the specified string. If the string is null, then the string "null" is appended.

Parameters
str the string to append.
Returns
  • this builder.

public StringBuilder append (StringBuffer sb)

Appends the contents of the specified StringBuffer. If the StringBuffer is null, then the string "null" is appended.

Parameters
sb the StringBuffer to append.
Returns
  • this builder.

public StringBuilder append (float f)

Appends the string representation of the specified float value. The float value is converted to a string according to the rule defined by valueOf(float).

Parameters
f the float value to append.
Returns
  • this builder.
See Also

public StringBuilder append (int i)

Appends the string representation of the specified int value. The int value is converted to a string according to the rule defined by valueOf(int).

Parameters
i the int value to append.
Returns
  • this builder.
See Also

public StringBuilder append (CharSequence csq, int start, int end)

Appends the string representation of the specified subsequence of the CharSequence. If the CharSequence is null, then the string "null" is used to extract the subsequence from.

Parameters
csq the CharSequence to append.
start the beginning index.
end the ending index.
Returns
  • this builder.
Throws
IndexOutOfBoundsException if start or end are negative, start is greater than end or end is greater than the length of csq.

public StringBuilder append (CharSequence csq)

Appends the string representation of the specified CharSequence. If the CharSequence is null, then the string "null" is appended.

Parameters
csq the CharSequence to append.
Returns
  • this builder.

public StringBuilder appendCodePoint (int codePoint)

Appends the encoded Unicode code point. The code point is converted to a char[] as defined by toChars(int).

Parameters
codePoint the Unicode code point to encode and append.
Returns
  • this builder.
See Also

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)

public int codePointBefore (int i)

public int codePointCount (int i, int j)

public StringBuilder delete (int start, int end)

Deletes a sequence of characters specified by start and end. Shifts any remaining characters to the left.

Parameters
start the inclusive start index.
end the exclusive end index.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if start is less than zero, greater than the current length or greater than end.

public StringBuilder deleteCharAt (int index)

Deletes the character at the specified index. shifts any remaining characters to the left.

Parameters
index the index of the character to delete.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if index is less than zero or is greater than or equal to the current length.

public void ensureCapacity (int i)

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

public int indexOf (String s)

public int indexOf (String s, int i)

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

Inserts the string representation of the specified char[] at the specified offset. The char[] value is converted to a String according to the rule defined by valueOf(char[]).

Parameters
offset the index to insert at.
ch the char[] to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length().
See Also

public StringBuilder insert (int offset, float f)

Inserts the string representation of the specified float value at the specified offset. The float value is converted to a string according to the rule defined by valueOf(float).

Parameters
offset the index to insert at.
f the float value to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length().
See Also

public StringBuilder insert (int offset, CharSequence s)

Inserts the string representation of the specified CharSequence at the specified offset. The CharSequence is converted to a String as defined by toString(). If s is null, then the String "null" is inserted.

Parameters
offset the index to insert at.
s the CharSequence to insert.
Returns
  • this builder.
Throws
IndexOutOfBoundsException if offset is negative or greater than the current length().
See Also

public StringBuilder insert (int offset, char c)

Inserts the string representation of the specified char value at the specified offset. The char value is converted to a string according to the rule defined by valueOf(char).

Parameters
offset the index to insert at.
c the char value to insert.
Returns
  • this builder.
Throws
IndexOutOfBoundsException if offset is negative or greater than the current length().
See Also

public StringBuilder insert (int offset, long l)

Inserts the string representation of the specified long value at the specified offset. The long value is converted to a String according to the rule defined by valueOf(long).

Parameters
offset the index to insert at.
l the long value to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current {code length()}.
See Also

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

Inserts the string representation of the specified subsequence of the char[] at the specified offset. The char[] value is converted to a String according to the rule defined by valueOf(char[], int, int).

Parameters
offset the index to insert at.
str the char[] to insert.
strOffset the inclusive index.
strLen the number of characters.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length(), or strOffset and strLen do not specify a valid subsequence.

public StringBuilder insert (int offset, int i)

Inserts the string representation of the specified int value at the specified offset. The int value is converted to a String according to the rule defined by valueOf(int).

Parameters
offset the index to insert at.
i the int value to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length().
See Also

public StringBuilder insert (int offset, String str)

Inserts the specified string at the specified offset. If the specified string is null, then the String "null" is inserted.

Parameters
offset the index to insert at.
str the String to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length().

public StringBuilder insert (int offset, double d)

Inserts the string representation of the specified double value at the specified offset. The double value is converted to a String according to the rule defined by valueOf(double).

Parameters
offset the index to insert at.
d the double value to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length().
See Also

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

Inserts the string representation of the specified subsequence of the CharSequence at the specified offset. The CharSequence is converted to a String as defined by subSequence(int, int). If the CharSequence is null, then the string "null" is used to determine the subsequence.

Parameters
offset the index to insert at.
s the CharSequence to insert.
start the start of the subsequence of the character sequence.
end the end of the subsequence of the character sequence.
Returns
  • this builder.
Throws
IndexOutOfBoundsException if offset is negative or greater than the current length(), or start and end do not specify a valid subsequence.

public StringBuilder insert (int offset, Object obj)

Inserts the string representation of the specified Object at the specified offset. The Object value is converted to a String according to the rule defined by valueOf(Object).

Parameters
offset the index to insert at.
obj the Object to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length().
See Also

public StringBuilder insert (int offset, boolean b)

Inserts the string representation of the specified boolean value at the specified offset. The boolean value is converted to a string according to the rule defined by valueOf(boolean).

Parameters
offset the index to insert at.
b the boolean value to insert.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if offset is negative or greater than the current length.
See Also

public int lastIndexOf (String s, int i)

public int lastIndexOf (String 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)

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

Replaces the specified subsequence in this builder with the specified string.

Parameters
start the inclusive begin index.
end the exclusive end index.
string the replacement string.
Returns
  • this builder.
Throws
StringIndexOutOfBoundsException if start is negative, greater than the current length() or greater than end.
NullPointerException if str is null.

public StringBuilder reverse ()

Reverses the order of characters in this builder.

Returns
  • this buffer.

public void setCharAt (int i, char c)

public void setLength (int i)

public CharSequence subSequence (int start, int end)

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
start the start index, inclusive
end the end index, exclusive
Returns
  • the specified subsequence

public String substring (int i, int j)

public String substring (int i)

public String toString ()

Returns the contents of this builder.

Returns
  • the string representation of the data in this builder.

public void trimToSize ()