Java.lang.StringBuilder.setCharAt() Method
Advertisements
Description
The java.lang.StringBuilder.setCharAt() method sets the character at the specified index to ch.The index argument must be greater than or equal to 0, and less than the length of this sequence.
Declaration
Following is the declaration for java.lang.StringBuilder.setCharAt() method
public void setCharAt(int index, char ch)
Parameters
index -- This is the index of the character to modify.
ch -- This is the new character.
Return Value
This method does not return any value.
Exception
IndexOutOfBoundsException -- if index is negative or greater than or equal to length().
Example
The following example shows the usage of java.lang.StringBuilder.setCharAt() method.
package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("AMIT");
System.out.println("string = " + str);
// character at index 3
System.out.println("character at index 3 = " + str.charAt(3));
// set character at index 3
str.setCharAt(3, 'L');
System.out.println("After Set, string = " + str);
// character at index 3
System.out.println("character at index 3 = " + str.charAt(3));
}
}
Let us compile and run the above program, this will produce the following result:
string = AMIT character at index 3 = T After Set, string = AMIL character at index 3 = L