Java StringBuilder Methods
All StringBuilder Methods
The StringBuilder class has a set of built-in methods that you can use to build and change text.
Most of these methods change the StringBuilder itself. The String methods return a new string instead, because a String cannot be changed.
| Method | Description | Return Type |
|---|---|---|
| append() | Adds text or a value to the end | StringBuilder |
| appendCodePoint() | Adds the character with the specified Unicode value to the end | StringBuilder |
| capacity() | Returns how many characters the StringBuilder can hold before it needs more memory | int |
| charAt() | Returns the character at the specified index (position) | char |
| codePointAt() | Returns the Unicode of the character at the specified index | int |
| codePointBefore() | Returns the Unicode of the character before the specified index | int |
| codePointCount() | Returns the number of Unicode values between two positions | int |
| delete() | Removes the characters between two positions | StringBuilder |
| deleteCharAt() | Removes the character at the specified index | StringBuilder |
| ensureCapacity() | Makes sure that the capacity is at least the specified value | void |
| getChars() | Copies characters into a char array | void |
| indexOf() | Returns the position of the first occurrence of the specified text | int |
| insert() | Inserts text or a value at the specified position | StringBuilder |
| lastIndexOf() | Returns the position of the last occurrence of the specified text | int |
| length() | Returns the number of characters | int |
| offsetByCodePoints() | Returns the index that is a number of code points away from a given index | int |
| replace() | Replaces the characters between two positions with new text | StringBuilder |
| reverse() | Reverses the order of the characters | StringBuilder |
| setCharAt() | Replaces the character at the specified index | void |
| setLength() | Sets the number of characters | void |
| subSequence() | Returns the characters between two positions as a CharSequence | CharSequence |
| substring() | Returns the characters between two positions as a new String | String |
| toString() | Returns the text as a String | String |
| trimToSize() | Reduces the capacity to fit the current text | void |