Java program to replace a character at a specific index in a string

Given a string, the task is to replace the character in a certain index in that string in Java.

Examples: 

Input: String = "Geeks Gor Geeks", index = 6, ch = 'F'
Output: "Geeks For Geeks."

Input: String = "Geeks", index = 0, ch = 'g'
Output: "geeks"

Method 1: Using String Class

There are currently no predefined methods in the String Class that can replace certain characters in a String. However, this can be achieved indirectly by creating a new string with 2 different substrings, one from start to specific index - 1, new character in specific index, and another from index +1 to end.
Below is an implementation of the above approach:

 

public class GFG {

	public static void main(String args[])
	{

		// Get the String
		String str = "Geeks Gor Geeks";

		// Get the index
		int index = 6;

		// Get the character
		char ch = 'F';

		// Print the original string
		System.out.println("Original String = " + str);

		str = str.substring(0, index) + ch
			+ str.substring(index + 1);

		// Print the modified string
		System.out.println("Modified String = " + str);
	}
}

Output

Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks

Method 2: Using StringBuilder

Unlike the String class, the StringBuilder class has a predefined method for this purpose - setCharAt(). Replace the character in the specified index by calling this method and passing the character and index as parameters.
Below is an implementation of the above approach:

 

public class GFG {

	public static void main(String args[])
	{

		// Get the String
		String str = "Geeks Gor Geeks";

		// Get the index
		int index = 6;

		// Get the character
		char ch = 'F';

		// Print the original string
		System.out.println("Original String = " + str);

		StringBuilder string = new StringBuilder(str);
		string.setCharAt(index, ch);

		// Print the modified string
		System.out.println("Modified String = " + string);
	}
}

Output

Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks

Method 3: Using StringBuffer

Like StringBuilder, the StringBuffer class features a predefined method for this purpose – setCharAt(). Replace the character at the precise index by calling this method and spending the character and therefore the index because the parameter. StringBuffer is thread-safe. StringBuilder is quicker in comparison to StringBuffer, but isn't thread-safe.

 

public class GFG {

	public static void main(String args[])
	{

		// Get the String
		String str = "Geeks Gor Geeks";

		// Get the index
		int index = 6;

		// Get the character
		char ch = 'F';

		// Print the original string
		System.out.println("Original String = " + str);

		StringBuffer string = new StringBuffer(str);
		string.setCharAt(index, ch);

		// Print the modified string
		System.out.println("Modified String = " + string);
	}
}

Output

Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks

 

Submit Your Programming Assignment Details