Top Description Fields Constructors Methods
java.lang

public final Class StringBuilder

extends AbstractStringBuilder
implements Appendable, Serializable, Comparable<StringBuilder>, CharSequence
Class Inheritance
All Implemented Interfaces
java.lang.CharSequence, java.lang.Comparable, java.io.Serializable, java.lang.Appendable
Imports
jdk.internal.vm.annotation.IntrinsicCandidate, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, .StreamCorruptedException

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 java.lang.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.

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.

Author
Michael McCloskey
Since
1.5
See Also
java.lang.StringBuffer, java.lang.String

Field Summary

Modifier and TypeField and Description
pack-priv static final long
serialVersionUID

use serialVersionUID for interoperability

Inherited from java.lang.AbstractStringBuilder:
codercountmaybeLatin1value

Constructor Summary

AccessConstructor 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 capacity argument.

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 CharSequence.

Method Summary

Modifier and TypeMethod and Description
public StringBuilder
append(Object
an Object.
obj
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the Object argument.
public StringBuilder
append(String
a string.
str
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the specified string to this character sequence.
public StringBuilder

Returns:

a reference to this object.
append
(StringBuffer
the StringBuffer to append.
sb
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the specified StringBuffer to this sequence.
public StringBuilder
append(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this Appendable.
s
)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this Appendable.
public StringBuilder
append(CharSequence
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".
s
,
int
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.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this Appendable.
public StringBuilder
append(char[]
the characters to be appended.
str
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the char array argument to this sequence.
public StringBuilder
append(char[]
the characters to be appended.
str
,
int
the index of the first char to append.
offset
,
int
the number of chars to append.
len
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of a subarray of the char array argument to this sequence.
public StringBuilder
append(boolean
a boolean.
b
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the boolean argument to the sequence.
public StringBuilder
append(char
The character to append
c
)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends the specified character to this Appendable.
public StringBuilder
append(int
an int.
i
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the int argument to this sequence.
public StringBuilder
append(long
a long.
lng
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the long argument to this sequence.
public StringBuilder
append(float
a float.
f
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the float argument to this sequence.
public StringBuilder
append(double
a double.
d
)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the double argument to this sequence.
public StringBuilder
appendCodePoint(int
a Unicode code point
codePoint
)

Overrides java.lang.AbstractStringBuilder.appendCodePoint.

Appends the string representation of the codePoint argument to this sequence.
public int

Returns:

the value 0 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.
compareTo
(StringBuilder
the StringBuilder to be compared with
another
)

Implements java.lang.Comparable.compareTo.

Compares two StringBuilder instances lexicographically.
public StringBuilder
delete(int
The beginning index, inclusive.
start
,
int
The ending index, exclusive.
end
)

Overrides java.lang.AbstractStringBuilder.delete.

Removes the characters in a substring of this sequence.
public StringBuilder
deleteCharAt(int
Index of char to remove
index
)

Overrides java.lang.AbstractStringBuilder.deleteCharAt.

Removes the char at the specified position in this sequence.
public int
indexOf(String
the substring to search for.
str
)

Overrides java.lang.AbstractStringBuilder.indexOf.

Returns the index within this string of the first occurrence of the specified substring.
public int
indexOf(String
the substring to search for.
str
,
int
the index from which to start the search.
fromIndex
)

Overrides java.lang.AbstractStringBuilder.indexOf.

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
public StringBuilder
insert(int
position at which to insert subarray.
index
,
char[]
A char array.
str
,
int
the index of the first char in subarray to be inserted.
offset
,
int
the number of chars in the subarray to be inserted.
len
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of a subarray of the str array argument into this sequence.
public StringBuilder
insert(int
the offset.
offset
,
Object
an Object.
obj
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the Object argument into this character sequence.
public StringBuilder
insert(int
the offset.
offset
,
String
a string.
str
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string into this character sequence.
public StringBuilder
insert(int
the offset.
offset
,
char[]
a character array.
str
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char array argument into this sequence.
public StringBuilder
insert(int
the offset.
dstOffset
,
CharSequence
the sequence to be inserted
s
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the specified 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.lang.AbstractStringBuilder.insert.

Inserts a subsequence of the specified CharSequence into this sequence.
public StringBuilder
insert(int
the offset.
offset
,
boolean
a boolean.
b
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the boolean argument into this sequence.
public StringBuilder
insert(int
the offset.
offset
,
char
a char.
c
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char argument into this sequence.
public StringBuilder
insert(int
the offset.
offset
,
int
an int.
i
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the second int argument into this sequence.
public StringBuilder
insert(int
the offset.
offset
,
long
a long.
l
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the long argument into this sequence.
public StringBuilder
insert(int
the offset.
offset
,
float
a float.
f
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the float argument into this sequence.
public StringBuilder
insert(int
the offset.
offset
,
double
a double.
d
)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the double argument into this sequence.
public int
lastIndexOf(String
the substring to search for.
str
)

Overrides java.lang.AbstractStringBuilder.lastIndexOf.

Returns the index within this string of the last occurrence of the specified substring.
public int
lastIndexOf(String
the substring to search for.
str
,
int
the index to start the search from.
fromIndex
)

Overrides java.lang.AbstractStringBuilder.lastIndexOf.

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 ObjectInputStream from which data is read
s
)

readObject 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.lang.AbstractStringBuilder.repeat.

Repeats count copies of the string representation of the codePoint argument to this sequence.
public StringBuilder
repeat(CharSequence
a CharSequence
cs
,
int
number of times to copy
count
)

Overrides java.lang.AbstractStringBuilder.repeat.

Appends 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.lang.AbstractStringBuilder.replace.

Replaces the characters in a substring of this sequence with characters in the specified String.
public StringBuilder
reverse()

Overrides java.lang.AbstractStringBuilder.reverse.

Causes this character sequence to be replaced by the reverse of the sequence.
public String
toString()

Implements abstract java.lang.AbstractStringBuilder.toString.

Implements java.lang.CharSequence.toString.

Returns a string containing the characters in this sequence in the same order as this sequence.
private void
writeObject(ObjectOutputStream
the ObjectOutputStream to which data is written
s
)

Save the state of the StringBuilder instance to a stream (that is, serialize it).

Inherited from java.lang.AbstractStringBuilder:
appendcapacitycharAtcharscodePointAtcodePointBeforecodePointCountcodePointscompareToensureCapacitygetBytesgetCharsgetCodergetValueinitBytesisLatin1lengthmixoffsetByCodePointsprependsetCharAtsetLengthsubSequencesubstringsubstringtrimToSize