using System.Diagnostics;
using System.Globalization;
public static class Program
public static string UnicodeReplaceAt(this string str, int offset, char replaceChar)
string replaceBy = replaceChar.ToString();
return new StringInfo(str).ReplaceByPosition(replaceBy, offset, count).String;
public static StringInfo ReplaceByPosition(this StringInfo str, string replaceBy, int offset, int count)
return str.RemoveByTextElements(offset, count).InsertByTextElements(offset, replaceBy);
public static StringInfo RemoveByTextElements(this StringInfo str, int offset, int count)
return new StringInfo(string.Concat(
str.SubstringByTextElements(0, offset),
offset + count < str.LengthInTextElements
? str.SubstringByTextElements(offset + count, str.LengthInTextElements - count - offset)
public static StringInfo InsertByTextElements(this StringInfo str, int offset, string insertStr)
if (string.IsNullOrEmpty(str.String))
return new StringInfo(insertStr);
return new StringInfo(string.Concat(
str.SubstringByTextElements(0, offset),
str.LengthInTextElements - offset > 0 ? str.SubstringByTextElements(offset, str.LengthInTextElements - offset) : ""
public static string SubsituteStringStringBuilder(this string s, int idx, char replaceChar)
if (string.IsNullOrEmpty(s) || idx >= s.Length || idx < 0)
return new StringBuilder(s).Remove(idx, 1).Insert(idx, replaceChar.ToString()).ToString();
public static string ReplaceAtSubstring(this string s, int idx, char replaceChar)
if (string.IsNullOrEmpty(s) || idx >= s.Length || idx < 0)
return s.Substring(0,idx) + replaceChar.ToString() + s.Substring(idx+replaceChar.ToString().Length,s.Length - (idx+replaceChar.ToString().Length) );
public static string ReplaceAtStringManipulation(this string s, int idx, char replaceChar)
if (string.IsNullOrEmpty(s) || idx >= s.Length || idx < 0)
return s.Remove(idx, 1).Insert(idx, replaceChar.ToString());
public static string ReplaceAtLinq(this string value, int index, char newchar)
if (value.Length <= index)
return string.Concat(value.Select((c, i) => i == index ? newchar : c));
public static string ReplaceAtCharArray(this string input, uint index, char newChar)
if (string.IsNullOrEmpty(input) || index >= input.Length)
char[] chars = input.ToCharArray();
return new string(chars);
public static void Main()
Stopwatch sw = new Stopwatch();
Console.WriteLine("ReplaceAtCharArraySSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSƾƾƾƾ".ReplaceAtCharArray(70,'X'));
Console.WriteLine("in {0} ticks.",sw.ElapsedTicks.ToString("N0"));
Console.WriteLine("ReplaceAtLinqSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSƾƾƾƾ".ReplaceAtLinq(70,'Y'));
Console.WriteLine("in {0} ticks.",sw.ElapsedTicks.ToString("N0"));
Console.WriteLine("ReplaceAtStringManipulationSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSƾƾƾƾ".ReplaceAtStringManipulation(70,'Z'));
Console.WriteLine("in {0} ticks.",sw.ElapsedTicks.ToString("N0"));
Console.WriteLine("ReplaceAtSubstringgggggggggSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSƾƾƾƾ".ReplaceAtSubstring(70,'R'));
Console.WriteLine("in {0} ticks.",sw.ElapsedTicks.ToString("N0"));
Console.WriteLine("SubsituteStringStringBuilderSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSƾƾƾƾ".SubsituteStringStringBuilder(70,'Ɯ'));
Console.WriteLine("in {0} ticks.",sw.ElapsedTicks.ToString("N0"));
Console.WriteLine("UnicodeReplaceAtSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSƾƾƾƾ".UnicodeReplaceAt(50,'Ɯ'));
Console.WriteLine("in {0} ticks.",sw.ElapsedTicks.ToString("N0"));