ExtensionMethod.NET Home of 828 C#, Visual Basic, F# and Javascript extension methods

Replace char at index

Replace one char at index

Source

public static string ReplaceAtIndex(this string text, int index, char c)
{
    var stringBuilder = new StringBuilder(text);
    stringBuilder[index] = c;
    return stringBuilder.ToString();
}

Example

Console.WriteLine("012345".ReplaceAtIndex(1,'I').ReplaceAtIndex(3,'T'));
// Output: 0I2T45

Author: mikelgusev

Submitted on: 27 jul 2020

Language: csharp

Type: System.String

Views: 213