Replace a character at a specific index in a String in Java
This post will discuss how to replace a character at a specific index in a Java String.
The string is an immutable class in Java. That means we cannot make any change in the String object. The only feasible solution is to create a new String object with the replaced character. There are several ways to replace a character at a specific index in a string:
1. Using substring() method
We can use String.substring(int, int) method to partition the string into two halves consisting of substring before and after the character to be replaced. Once we have isolated the character to be replaced, we can use the concatenation operator to build the final string, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
class Main { public static void main(String[] args) { String str = "Techie Delight"; char ch = '_'; int pos = 6; str = str.substring(0, pos) + ch + str.substring(pos + 1); System.out.println(str); } } |
Output:
Techie_Delight
2. Using StringBuilder
The recommended solution is to use mutable class StringBuilder to efficiently replace a character at a specific index in a string in Java. Alternatively, we can also use a slower StringBuffer class if thread safety is required.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Main { public static void main(String[] args) { String str = "Techie Delight"; char ch = '_'; int pos = 6; StringBuilder sb = new StringBuilder(str); // replace character at the specified position sb.setCharAt(pos, ch); str = sb.toString(); // print the modified string System.out.println(str); } } |
Output:
Techie_Delight
3. Using toCharArray() method
Another plausible way of replacing character at the specified index in a string is using a character array that can be modified easily. The idea is to convert the given string to a character array using its toCharArray() method and then replace the character at the given index in the character array. Finally, convert the character array back into a string using String.valueOf(char[]) method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Main { public static void main(String[] args) { String str = "Techie Delight"; char ch = '_'; int pos = 6; // convert the given string to a character array char[] chars = str.toCharArray(); // replace character at the specified position in a char array chars[pos] = ch; // convert the character array back into a string str = String.valueOf(chars); // print the modified string System.out.println(str); } } |
Output:
Techie_Delight
4. Using Reflection
We have seen that we cannot make any change in the String object as string is immutable in Java. However, there is a way to modify a string using reflection. Reflection in Java allows code to perform illegal operations such as accessing and manipulating private fields and methods.
We know that string internally uses a character array that is final and private to the class. Although not recommended, reflection can easily modify that private character array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.lang.reflect.Field; class Main { public static void main(String[] args) { String str = "Techie Delight"; char ch = '_'; int pos = 6; Field field = null; try { // The `value[]` is used for character storage in the `String` class. field = String.class.getDeclaredField("value"); } catch (NoSuchFieldException e) { e.printStackTrace(); } // set value[] accessible since it has a private scope field.setAccessible(true); char[] chars = new char[0]; try { // get the value of the field chars = (char[])field.get(str); } catch (IllegalAccessException e) { e.printStackTrace(); } // replace character at the specified position chars[pos] = ch; // print the modified string System.out.println(str); } } |
Output:
Techie_Delight
That’s all about replacing a character at a specific index in a Java String.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)