replace string by index of this string [closed]

string content = File.ReadAllText("data.txt");
var replacements = new[] { "A", "B", "A" };

int index = 0;
string result = Regex.Replace(content, @"[a-zA-Z]+",
                              m => replacements.Length > index ? 
                                   replacements[index++] : m.Value);

This will execute MatchEvaluator for each found word, and replace it with value from appropriate position in replacements array.

Leave a Comment