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.
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 using 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 = " + string);
string = string.substring(0, index) + ch
+ string.substring(index + 1);
// Print the modified string
System.out.println("Modified String = " + string);
}
}
Original String = How ate you?
Modified String = How are you?
Example 2: Using StringBuilder setCharAt() method
The StringBuilder setCharAt() method sets a specified character at the given index. This method changes the character sequence represented by StringBuilder object as it replaces the existing char with new char.
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 = " + string);
StringBuilder result = new StringBuilder(string);
result.setCharAt(index, ch);
// Print the modified string
System.out.println("Modified String = " + result);
}
}
Original String = How ate you?
Modified String = How are you?
Example 3: Using StringBuffer setCharAt() method
The StringBuffer setCharAt() method sets a specified character at the given index. This method changes the character sequence represented by StringBuilder object as it replaces the existing char with new char.
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 = " + string);
StringBuffer result = new StringBuffer(string);
result.setCharAt(index, ch);
// Print the modified string
System.out.println("Modified String = " + result);
}
}
Original String = How ate you?
Modified String = How are you?
Example 4: Using toCharArray() method
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 = " + string);
// convert the given string to a character array
char[] chars = string.toCharArray();
// replace character at the specified position in a char array
chars[index] = ch;
// convert the character array back into a string
string = String.valueOf(chars);
// Print the modified string
System.out.println("Modified String = " + string);
}
}
Original String = How ate you?
Modified String = How are you?