Replace a character at a specific index in a String in Java

Given a String, the task is to replace a character at a specific index in this 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 is no predefined method in String Class to replace a specific character in a String, as of now. However this can be achieved indirectly by constructing new String with the 2 different substrings: one from beginning till the specific index – 1, the new character at the specific index and the other from the index + 1 till the end.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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);
        }
    }

    chevron_right

    
    

    Output:



    Original String = Geeks Gor Geeks
    Modified String = Geeks For Geeks
    
  • Method 2: Using StringBuilder

    Unlike String Class, StringBuilder class has a predefined method for this purpose – setCharAt(). Replace the character at the specific index by calling this method and passing the character and the index as the parameter.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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);
        }
    }

    chevron_right

    
    

    Output:

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


My Personal Notes arrow_drop_up

Technical Content Engineer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.