StringBuilder
class StringBuilder : Appendable, CharSequence, Comparable<StringBuilder!>, Serializable
| kotlin.Any | |
| ↳ | java.lang.StringBuilder |
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.
Summary
| Public constructors | |
|---|---|
|
Constructs a string builder with no characters in it and an initial capacity of 16 characters. |
|
StringBuilder(capacity: Int)Constructs a string builder with no characters in it and an initial capacity specified by the |
|
StringBuilder(seq: CharSequence)Constructs a string builder that contains the same characters as the specified |
|
StringBuilder(str: String)Constructs a string builder initialized to the contents of the specified string. |
|
| Public methods | |
|---|---|
| StringBuilder | |
| StringBuilder |
Appends the specified character to this |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder |
append(s: CharSequence?)Appends the specified character sequence to this |
| StringBuilder |
append(s: CharSequence?, start: Int, end: Int) |
| StringBuilder | |
| StringBuilder | |
| StringBuilder |
append(sb: StringBuffer?)Appends the specified |
| StringBuilder | |
| StringBuilder |
appendCodePoint(codePoint: Int) |
| Int |
capacity()Returns the current capacity. |
| IntStream |
chars()Returns a stream of |
| Int |
codePointAt(index: Int)Returns the character (Unicode code point) at the specified index. |
| Int |
codePointBefore(index: Int)Returns the character (Unicode code point) before the specified index. |
| Int |
codePointCount(beginIndex: Int, endIndex: Int)Returns the number of Unicode code points in the specified text range of this sequence. |
| IntStream |
Returns a stream of code point values from this sequence. |
| Int |
compareTo(other: StringBuilder)Compares two |
| StringBuilder | |
| StringBuilder |
deleteCharAt(index: Int) |
| Unit |
ensureCapacity(minimumCapacity: Int)Ensures that the capacity is at least equal to the specified minimum. |
| Char |
Returns the |
| Unit |
Copies characters from this sequence into the given destination array. |
| Int | |
| Int | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| StringBuilder |
insert(dstOffset: Int, s: CharSequence?) |
| StringBuilder |
insert(dstOffset: Int, s: CharSequence?, start: Int, end: Int) |
| StringBuilder | |
| StringBuilder | |
| StringBuilder | |
| Int |
lastIndexOf(str: String) |
| Int |
lastIndexOf(str: String, fromIndex: Int) |
| Int |
offsetByCodePoints(index: Int, codePointOffset: Int)Returns the index within this sequence that is offset from the given |
| StringBuilder | |
| StringBuilder |
repeat(cs: CharSequence, count: Int) |
| StringBuilder | |
| StringBuilder |
reverse() |
| Unit |
The character at the specified index is set to |
| Unit |
Sets the length of the character sequence. |
| CharSequence |
subSequence(startIndex: Int, endIndex: Int)Returns a new character sequence that is a subsequence of this sequence. |
| String |
Returns a new |
| String |
Returns a new |
| String |
toString()Returns a string representation of the object. |
| Unit |
Attempts to reduce storage used for the character sequence. |
| Properties | |
|---|---|
| Int |
Returns the length (character count). |
Public constructors
StringBuilder
StringBuilder()
Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder
StringBuilder(capacity: Int)
Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
| Parameters | |
|---|---|
capacity |
Int: the initial capacity. |
| Exceptions | |
|---|---|
java.lang.NegativeArraySizeException |
if the capacity argument is less than 0. |
StringBuilder
StringBuilder(seq: CharSequence)
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. |
StringBuilder
StringBuilder(str: String)
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. |
Public methods
append
fun append(c: Char): StringBuilder
Appends the specified character to this Appendable.
| Parameters | |
|---|---|
c |
Char: The character to append |
| Return | |
|---|---|
StringBuilder |
A reference to this Appendable |
| Exceptions | |
|---|---|
java.io.IOException |
If an I/O error occurs |
append
fun append(
str: CharArray!,
offset: Int,
len: Int
): StringBuilder
| Exceptions | |
|---|---|
java.lang.IndexOutOfBoundsException |
|