Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmReplace a character at a specific index in a String in Java

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

 In Java, here we are given a string, the task is to replace a character at a specific index in this string.

Examples: 

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

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 a new String with 2 different substrings, one from the 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: 

Java




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 String Class, the StringBuilder class is used to represent a mutable string of characters and has a predefined method for change a character at a specific index – 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: 

Java




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 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. StringBuffer is thread-safe and can be used in a multi-threaded environment. StringBuilder is faster when compared to StringBuffer, but is not thread-safe.

Below is the implementation of the above approach:

Java




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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments