How to replace a character at a specific index in a String in Java
In this section, we will show you how replace a character at a specific index in a String in Java. Note: String is an immutable class in java. That means we cannot make any change in the String object. Any method which seems to modify it always returns a new string object with modification. Following ways can be used to replace a character at a specific index in a String in Java: By u sing substring () method By using StringBuilder setCharAt() method By using StringBuffer setCharAt() method By using toCharArray() method Example 1: Using substring () method Create a new string with the character replaced. public class Main { // Driver code public static void main ( String [] args) { // Get the String String string = "How ate you?" ; // Get the index int index = 5 ; // Get the character char ch = 'r' ; // Print the original string System . out .println( "Original String = " + s