APIdia Launch Partner
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 obj)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the Object argument.

public StringBuilder
append(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 s)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this Appendable.

public StringBuilder
append(CharSequence s, int start, int 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[] str)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the char array argument to this sequence.

public StringBuilder
append(char[] str, int offset, int 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 b)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the boolean argument to the sequence.

public StringBuilder
append(char c)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Appends the specified character to this Appendable.

public StringBuilder
append(int i)

Overrides java.lang.AbstractStringBuilder.append.

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

public StringBuilder
append(long lng)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the long argument to this sequence.

public StringBuilder
append(float f)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the float argument to this sequence.

public StringBuilder
append(double d)

Overrides java.lang.AbstractStringBuilder.append.

Appends the string representation of the double argument to this sequence.

public StringBuilder
appendCodePoint(int 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 start, int end)

Overrides java.lang.AbstractStringBuilder.delete.

Removes the characters in a substring of this sequence.

public StringBuilder
deleteCharAt(int index)

Overrides java.lang.AbstractStringBuilder.deleteCharAt.

Removes the char at the specified position in this sequence.

public int
indexOf(String str)

Overrides java.lang.AbstractStringBuilder.indexOf.

Returns the index within this string of the first occurrence of the specified substring.

public int
indexOf(String str, int 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 index, char[] str, int offset, int 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 offset, Object obj)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the Object argument into this character sequence.

public StringBuilder
insert(int offset, String str)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string into this character sequence.

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

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char array argument into this sequence.

public StringBuilder
insert(int dstOffset, CharSequence s)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the specified CharSequence into this sequence.

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

Overrides java.lang.AbstractStringBuilder.insert.

Inserts a subsequence of the specified CharSequence into this sequence.

public StringBuilder
insert(int offset, boolean b)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the boolean argument into this sequence.

public StringBuilder
insert(int offset, char c)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char argument into this sequence.

public StringBuilder
insert(int offset, int i)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the second int argument into this sequence.

public StringBuilder
insert(int offset, long l)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the long argument into this sequence.

public StringBuilder
insert(int offset, float f)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the float argument into this sequence.

public StringBuilder
insert(int offset, double d)

Overrides java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the double argument into this sequence.

public int
lastIndexOf(String str)

Overrides java.lang.AbstractStringBuilder.lastIndexOf.

Returns the index within this string of the last occurrence of the specified substring.

public int
lastIndexOf(String str, int 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 codePoint, int count)

Overrides java.lang.AbstractStringBuilder.repeat.

Repeats count copies of the string representation of the codePoint argument to this sequence.

public StringBuilder
repeat(CharSequence cs, int count)

Overrides java.lang.AbstractStringBuilder.repeat.

Appends count copies of the specified CharSequence cs to this sequence.

public StringBuilder
replace(int start, int end, String 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:
appendcapacitycharAtcharscodePointAtcodePointBeforecodePointCountcodePointscompareToensureCapacitygetBytesgetCharsgetCodergetValueinitBytesisLatin1lengthoffsetByCodePointssetCharAtsetLengthsubSequencesubstringsubstringtrimToSize

Field Detail

serialVersionUIDback to summary
pack-priv static final long serialVersionUID

use serialVersionUID for interoperability

Annotations
@Serial

Constructor Detail

StringBuilderback to summary
public StringBuilder()

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

Annotations
@IntrinsicCandidate
StringBuilderback to summary
public StringBuilder(int capacity)

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

Parameters
capacity:int

the initial capacity.

Annotations
@IntrinsicCandidate
Exceptions
NegativeArraySizeException:
if the capacity argument is less than 0.
StringBuilderback 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 16 plus the length of the string argument.

Parameters
str:String

the initial contents of the buffer.

Annotations
@IntrinsicCandidate
StringBuilderback to summary
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:CharSequence

the sequence to copy.

Method Detail

appendback to summary
public StringBuilder append(Object obj)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the string representation of the Object argument.

The overall effect is exactly as if the argument were converted to a string by the method String#valueOf(Object), and the characters of that string were then appended to this character sequence.

Annotations
@Override
appendback to summary
public StringBuilder append(String str)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the specified string to this character sequence.

The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are appended.

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

Annotations
@Override
@IntrinsicCandidate
appendback to summary
public StringBuilder append(StringBuffer sb)

Overrides java.lang.AbstractStringBuilder.append.

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:StringBuffer

the StringBuffer to append.

Returns:StringBuilder

a reference to this object.

appendback to summary
public StringBuilder append(CharSequence s)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Doc from java.lang.Appendable.append.

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.

The contents of this Appendable are unspecified if the CharSequence is modified during the method call or an exception is thrown when accessing the CharSequence.

Annotations
@Override
appendback to summary
public StringBuilder append(CharSequence s, int start, int end)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Doc from java.lang.Appendable.append.

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

The contents of this Appendable are unspecified if the CharSequence is modified during the method call or an exception is thrown when accessing the CharSequence.

Annotations
@Override
Exceptions
IndexOutOfBoundsException:
If start or end are negative, start is greater than end, or end is greater than csq.length()
appendback to summary
public StringBuilder append(char[] str)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the string representation of the char array argument to this sequence.

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 String#valueOf(char[]), and the characters of that string were then appended to this character sequence.

Annotations
@Override
appendback to summary
public StringBuilder append(char[] str, int offset, int len)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the string representation of a subarray of the char array argument to this sequence.

Characters of the char array str, starting at index offset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value of len.

The overall effect is exactly as if the arguments were converted to a string by the method String#valueOf(char[], int, int), and the characters of that string were then appended to this character sequence.

Annotations
@Override
Exceptions
IndexOutOfBoundsException:
if offset < 0 or len < 0 or offset+len > str.length
appendback to summary
public StringBuilder append(boolean b)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the string representation of the boolean argument to the sequence.

The overall effect is exactly as if the argument were converted to a string by the method String#valueOf(boolean), and the characters of that string were then appended to this character sequence.

Annotations
@Override
appendback to summary
public StringBuilder append(char c)

Overrides java.lang.AbstractStringBuilder.append.

Implements java.lang.Appendable.append.

Doc from java.lang.Appendable.append.

Appends the specified character to this Appendable.

Annotations
@Override
@IntrinsicCandidate
appendback to summary
public StringBuilder append(int i)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

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

The overall effect is exactly as if the argument were converted to a string by the method String#valueOf(int), and the characters of that string were then appended to this character sequence.

Annotations
@Override
@IntrinsicCandidate
appendback to summary
public StringBuilder append(long lng)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the string representation of the long argument to this sequence.

The overall effect is exactly as if the argument were converted to a string by the method String#valueOf(long), and the characters of that string were then appended to this character sequence.

Annotations
@Override
appendback to summary
public StringBuilder append(float f)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the string representation of the float argument to this sequence.

The overall effect is exactly as if the argument were converted to a string by the method String#valueOf(float), and the characters of that string were then appended to this character sequence.

Annotations
@Override
appendback to summary
public StringBuilder append(double d)

Overrides java.lang.AbstractStringBuilder.append.

Doc from java.lang.AbstractStringBuilder.append.

Appends the string representation of the double argument to this sequence.

The overall effect is exactly as if the argument were converted to a string by the method String#valueOf(double), and the characters of that string were then appended to this character sequence.

Annotations
@Override
appendCodePointback to summary
public StringBuilder appendCodePoint(int codePoint)

Overrides java.lang.AbstractStringBuilder.appendCodePoint.

Doc from java.lang.AbstractStringBuilder.appendCodePoint.

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

The argument is appended to the contents of this sequence. The length of this sequence increases by Character.charCount(codePoint).

The overall effect is exactly as if the argument were converted to a char array by the method Character#toChars(int) and the character in that array were then appended to this character sequence.

Annotations
@Override
Since
1.5
compareToback to summary
public int compareTo(StringBuilder another)

Implements java.lang.Comparable.compareTo.

Compares two StringBuilder instances lexicographically. This method follows the same rules for lexicographical comparison as defined in the CharSequence.compare(this, another) method.

For finer-grained, locale-sensitive String comparison, refer to java.text.Collator.

Parameters
another:StringBuilder

the StringBuilder to be compared with

Returns:int

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.

Annotations
@Override
Since
11
deleteback to summary
public StringBuilder delete(int start, int end)

Overrides java.lang.AbstractStringBuilder.delete.

Doc from java.lang.AbstractStringBuilder.delete.

Removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. If start is equal to end, no changes are made.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if start is negative, greater than length(), or greater than end.
deleteCharAtback to summary
public StringBuilder deleteCharAt(int index)

Overrides java.lang.AbstractStringBuilder.deleteCharAt.

Doc from java.lang.AbstractStringBuilder.deleteCharAt.

Removes the char at the specified position in this sequence. This sequence is shortened by one char.

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 chars to remove by calling Character.charCount(thisSequence.codePointAt(index)), where thisSequence is this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the index is negative or greater than or equal to length().
indexOfback to summary
public int indexOf(String str)

Overrides java.lang.AbstractStringBuilder.indexOf.

Doc from java.lang.AbstractStringBuilder.indexOf.

Returns the index within this string of the first occurrence of the specified substring.

The returned index is the smallest value k for which:

this.toString().startsWith(str, k)
If no such value of k exists, then -1 is returned.
Annotations
@Override
indexOfback to summary
public int indexOf(String str, int fromIndex)

Overrides java.lang.AbstractStringBuilder.indexOf.

Doc from java.lang.AbstractStringBuilder.indexOf.

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 k for which:

k >= Math.min(fromIndex, this.length()) &&
                  this.toString().startsWith(str, k)
If no such value of k exists, then -1 is returned.
Annotations
@Override
insertback to summary
public StringBuilder insert(int index, char[] str, int offset, int len)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of a subarray of the str array argument into this sequence. The subarray begins at the specified offset and extends len chars. The characters of the subarray are inserted into this sequence at the position indicated by index. The length of this sequence increases by len chars.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if index is negative or greater than length(), or offset or len are negative, or (offset+len) is greater than str.length.
insertback to summary
public StringBuilder insert(int offset, Object obj)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the Object argument into this character sequence.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(Object), and the characters of that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int offset, String str)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string into this character sequence.

The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are inserted into this sequence.

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 offset
  • the character at index k-offset in the argument str, if k is not less than offset but is less than offset+str.length()
  • the character at index k-str.length() in the old character sequence, if k is not less than offset+str.length()

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int offset, char[] str)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char array argument into this sequence.

The characters of the array argument are inserted into the contents of this sequence at the position indicated by offset. The length of this sequence increases by the length of the argument.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(char[]), and the characters of that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int dstOffset, CharSequence s)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the specified CharSequence into this sequence.

The characters of the CharSequence argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument s.

The result of this method is exactly the same as if it were an invocation of this object's insert(dstOffset, s, 0, s.length()) method.

The contents are unspecified if the CharSequence is modified during the method call or an exception is thrown when accessing the CharSequence.

If s is null, then the four characters "null" are inserted into this sequence.

Annotations
@Override
Exceptions
IndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts a subsequence of the specified CharSequence into this sequence.

The subsequence of the argument s specified by start and end are inserted, in order, into this sequence at the specified destination offset, moving up any characters originally above that position. The length of this sequence is increased by end - start.

The character at index k in this sequence becomes equal to:

  • the character at index k in this sequence, if k is less than dstOffset
  • the character at index k+start-dstOffset in the argument s, if k is greater than or equal to dstOffset but is less than dstOffset+end-start
  • the character at index k-(end-start) in this sequence, if k is greater than or equal to dstOffset+end-start

The dstOffset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

The start argument must be non-negative, and not greater than end.

The end argument must be greater than or equal to start, and less than or equal to the length of s.

If s is null, then this method inserts characters as if the s parameter was a sequence containing the four characters "null".

The contents are unspecified if the CharSequence is modified during the method call or an exception is thrown when accessing the CharSequence.

Annotations
@Override
Exceptions
IndexOutOfBoundsException:
if dstOffset is negative or greater than this.length(), or start or end are negative, or start is greater than end or end is greater than s.length()
insertback to summary
public StringBuilder insert(int offset, boolean b)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the boolean argument into this sequence.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(boolean), and the characters of that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int offset, char c)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the char argument into this sequence.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(char), and the character in that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
IndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int offset, int i)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the second int argument into this sequence.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(int), and the characters of that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int offset, long l)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the long argument into this sequence.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(long), and the characters of that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int offset, float f)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the float argument into this sequence.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(float), and the characters of that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
insertback to summary
public StringBuilder insert(int offset, double d)

Overrides java.lang.AbstractStringBuilder.insert.

Doc from java.lang.AbstractStringBuilder.insert.

Inserts the string representation of the double argument into this sequence.

The overall effect is exactly as if the second argument were converted to a string by the method String#valueOf(double), and the characters of that string were then inserted into this character sequence at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if the offset is invalid.
lastIndexOfback to summary
public int lastIndexOf(String str)

Overrides java.lang.AbstractStringBuilder.lastIndexOf.

Doc from java.lang.AbstractStringBuilder.lastIndexOf.

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 this.length().

The returned index is the largest value k for which:

this.toString().startsWith(str, k)
If no such value of k exists, then -1 is returned.
Annotations
@Override
lastIndexOfback to summary
public int lastIndexOf(String str, int fromIndex)

Overrides java.lang.AbstractStringBuilder.lastIndexOf.

Doc from 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.

The returned index is the largest value k for which:

k <= Math.min(fromIndex, this.length()) &&
                  this.toString().startsWith(str, k)
If no such value of k exists, then -1 is returned.
Annotations
@Override
readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException

readObject is called to restore the state of the StringBuilder from a stream.

Parameters
s:ObjectInputStream

the ObjectInputStream from which data is read

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
ClassNotFoundException:
if a serialized class cannot be loaded
repeatback to summary
public StringBuilder repeat(int codePoint, int count)

Overrides java.lang.AbstractStringBuilder.repeat.

Doc from java.lang.AbstractStringBuilder.repeat.

Repeats count copies of the string representation of the codePoint argument to this sequence.

The length of this sequence increases by count times the string representation length.

It is usual to use char expressions for code points. For example:

// insert 10 asterisks into the buffer sb.repeat('*', 10);
// insert 10 asterisks into the buffer
sb.repeat('*', 10);
Annotations
@Override
Exceptions
IllegalArgumentException:
if the specified codePoint is not a valid Unicode code point or if count is negative.
Since
21
repeatback to summary
public StringBuilder repeat(CharSequence cs, int count)

Overrides java.lang.AbstractStringBuilder.repeat.

Doc from java.lang.AbstractStringBuilder.repeat.

Appends count copies of the specified CharSequence cs to this sequence.

The length of this sequence increases by count times the CharSequence length.

If cs is null, then the four characters "null" are repeated into this sequence.

The contents are unspecified if the CharSequence is modified during the method call or an exception is thrown when accessing the CharSequence.

Annotations
@Override
Exceptions
IllegalArgumentException:
if count is negative
Since
21
replaceback to summary
public StringBuilder replace(int start, int end, String str)

Overrides java.lang.AbstractStringBuilder.replace.

Doc from java.lang.AbstractStringBuilder.replace.

Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.)

Annotations
@Override
Exceptions
StringIndexOutOfBoundsException:
if start is negative, greater than length(), or greater than end.
reverseback to summary
public StringBuilder reverse()

Overrides java.lang.AbstractStringBuilder.reverse.

Doc from java.lang.AbstractStringBuilder.reverse.

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 char values) just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.

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.

Annotations
@Override
toStringback to summary
public String toString()

Implements abstract java.lang.AbstractStringBuilder.toString.

Implements java.lang.CharSequence.toString.

Doc from java.lang.CharSequence.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.

Annotations
@Override
@IntrinsicCandidate
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

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

Parameters
s:ObjectOutputStream

the ObjectOutputStream to which data is written

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
Serial data
the number of characters currently stored in the string builder (int), followed by the characters in the string builder (char[]). The length of the char array may be greater than the number of characters currently stored in the string builder, in which case extra characters are ignored.