Javatpoint Logo
Javatpoint Logo

Java StringBuilder setCharAt() method

The setCharAt(int index, char ch) method of Java StringBuilder class is used to set the specified character at the given index. The specified character altered this sequence by replacing the old character at the given index.

Syntax:

Parameter:

DataType Parameter Description
int index It is the index of character which is to modify.
char ch A new character which modifies the old character at the given index.

Returns:

NA

Exception:

IndexOutOfBoundsException - if an index is negative or greater than or equal to length() of this sequence.

Compatibility Version:

Java 1.5 and above

Example 1

Test it Now

Output:

string = abc
character at index 1 = b
new string = axc
character at index 1 = x

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
	at java.lang.AbstractStringBuilder.setCharAt(Unknown Source)
	at java.lang.StringBuilder.setCharAt(Unknown Source)
	at snippet.StringBuilderSetCharAtExample2.main(StringBuilderSetCharAtExample2.java:6)
string = abc 

Example 3

Test it Now

Output:

enter your string value: Java
enter index value: 0
enter character value: j
new string = java






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA