I have a text:
Hello abc Hello def Hello
I want to convert it to
Hello1 abc Hello2 abc Hello3
ie I need to add a number after each occurrence of the text "Hello".
I have currently written this code:
var xx = File.ReadAllText("D:\\test.txt"); var regex = new Regex("Hello", RegexOptions.IgnoreCase); var matches = regex.Matches(xx); int i = 1; foreach (var match in matches.Cast<Match>()) { string yy = match.Value; xx = Replace(xx, match.Index, match.Length, match.Value + (i++)); }
and the replacement method used above:
public static string Replace(string s, int index, int length, string replacement) { var builder = new StringBuilder(); builder.Append(s.Substring(0, index)); builder.Append(replacement); builder.Append(s.Substring(index + length)); return builder.ToString(); }
Currently, the above code does not work and replaces the text between them.
Can you help me fix this?
source share