You can use the dictionary to more quickly repeat Replace calls:
var replacements = new Dictionary<string, string>(); replacements.Add("echo", "print"); replacements.Add("<?php", "<?"); ... foreach(var pair in replacements) { myString = myString.Replace(pair.Key, pair.Value); }
Or use the Linq Aggregate method.
myString = replacements.Aggregate(myString, (s, p) => s.Replace(p.Key, p.Value));
This will work for simple strings, but the same general design can be used for regular expression patterns:
var replacements = new Dictionary<string, string>(); ... foreach(var pair in replacement) { myString = new RegEx(pair.Key).Replace(myString, pair.Value); }
And again with Linq:
myString = replacements.Aggregate(myString, (s, p) => new RegEx(pair.Key).Replace(s, p.Value));
source share