Java StringBuilder append() Method
Example
Add text to the end of a StringBuilder:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
Definition and Usage
The append() method adds text or a value to the end of a StringBuilder.
It works with strings, characters, numbers, booleans and objects. Values that are not text are converted to text first.
Tip: append() returns the same StringBuilder, so you can chain several calls: sb.append("Score: ").append(100);
Syntax
One of the following:
public StringBuilder append(type value)
public StringBuilder append(char[] str, int offset, int len)
public StringBuilder append(CharSequence s, int start, int end)
type can be String, char, int, long, float, double, boolean, char[], CharSequence, StringBuffer or Object.
Parameter Values
| Parameter | Description |
|---|---|
| value | Required. The value to add to the end. |
| str | A char array to take characters from. |
| offset | The index of the first character to take from the array. |
| len | The number of characters to take from the array. |
| s | A CharSequence, like a String, to take characters from. |
| start | The index of the first character to take from s. |
| end | The index after the last character to take from s. |
Technical Details
| Returns: | The same StringBuilder, with the value added to the end. |
|---|---|
| Throws: | IndexOutOfBoundsException - if the positions in the versions with offset or start are outside the array or text. |
Related Pages
❮ StringBuilder Methods