C# program to replace n-th character from a given index in a string

In C#, you can replace a character at a specific index in a string by converting the string to a character array, modifying the desired character, and then creating a new string from the modified array.

Syntax

Following is the basic syntax for replacing a character at a specific index −

char[] charArray = originalString.ToCharArray();
charArray[index] = newCharacter;
string newString = new string(charArray);

Using ToCharArray() Method

The most straightforward approach is to convert the string to a character array, replace the character at the desired index, and construct a new string −

using System;

public class Demo {
    public static void Main(string[] args) {
        string str1 = "Port";
        Console.WriteLine("Original String: " + str1);
        
        char[] ch = str1.ToCharArray();
        ch[0] = 'F';  // Replace character at index 0
        
        string str2 = new string(ch);
        Console.WriteLine("New String: " + str2);
        
        // Replace character at index 2
        string str3 = "Hello";
        Console.WriteLine("Original String: " + str3);
        char[] ch2 = str3.ToCharArray();
        ch2[2] = 'X';
        string str4 = new string(ch2);
        Console.WriteLine("New String: " + str4);
    }
}

The output of the above code is −

Original String: Port
New String: Fort
Original String: Hello
New String: HeXlo

Using String Concatenation

Another approach is to use string concatenation by combining the substring before the index, the new character, and the substring after the index −

using System;

public class Demo {
    public static void Main(string[] args) {
        string original = "Programming";
        int index = 5;
        char newChar = 'X';
        
        Console.WriteLine("Original String: " + original);
        Console.WriteLine("Replacing character at index " + index + " with '" + newChar + "'");
        
        string modified = original.Substring(0, index) + newChar + original.Substring(index + 1);
        Console.WriteLine("Modified String: " + modified);
        
        // Replace multiple characters at different positions
        string text = "ABCDEF";
        Console.WriteLine("\nOriginal: " + text);
        text = text.Substring(0, 1) + 'X' + text.Substring(2);  // Replace at index 1
        text = text.Substring(0, 3) + 'Y' + text.Substring(4);  // Replace at index 3
        Console.WriteLine("After replacements: " + text);
    }
}

The output of the above code is −

Original String: Programming
Replacing character at index 5 with 'X'
Modified String: ProgrXmming
Original: ABCDEF
After replacements: AXYDEF

Using StringBuilder for Multiple Replacements

For multiple character replacements, StringBuilder provides better performance by avoiding the creation of multiple string objects −

using System;
using System.Text;

public class Demo {
    public static void Main(string[] args) {
        string original = "Technology";
        Console.WriteLine("Original String: " + original);
        
        StringBuilder sb = new StringBuilder(original);
        sb[0] = 'M';  // Replace T with M
        sb[4] = 'X';  // Replace n with X
        sb[9] = 'Z';  // Replace y with Z
        
        string modified = sb.ToString();
        Console.WriteLine("Modified String: " + modified);
        
        // Create a method for single character replacement
        string result = ReplaceCharAt("Computer", 4, 'X');
        Console.WriteLine("Replace 'u' with 'X': " + result);
    }
    
    public static string ReplaceCharAt(string input, int index, char newChar) {
        if (index = input.Length) {
            return input;  // Return original if index is out of bounds
        }
        StringBuilder sb = new StringBuilder(input);
        sb[index] = newChar;
        return sb.ToString();
    }
}

The output of the above code is −

Original String: Technology
Modified String: MechXologZ
Replace 'u' with 'X': CompXter

Comparison of Methods

Method Best For Performance
ToCharArray() Simple single replacements Good for short strings
String Concatenation One-time replacements Creates temporary strings
StringBuilder Multiple replacements Best for multiple operations

Conclusion

Replacing characters at specific indices in C# can be accomplished using character arrays, string concatenation, or StringBuilder. The ToCharArray() method is ideal for simple replacements, while StringBuilder offers better performance for multiple character modifications on the same string.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements