Java StringBuilder setCharAt() Method
Example
Change the first character to "J":
StringBuilder sb = new StringBuilder("Hello");
sb.setCharAt(0, 'J');
System.out.println(sb);
Definition and Usage
The setCharAt() method replaces the character at the specified index with a new character.
Tip: Use charAt() to read a character.
Syntax
public void setCharAt(int index, char ch)
Parameter Values
| Parameter | Description |
|---|---|
| index | Required. The index of the character to change. |
| ch | Required. The new character. |
Technical Details
| Throws: | IndexOutOfBoundsException - if index is negative or not less than the length. |
|---|
Related Pages
❮ StringBuilder Methods