真正的男人使用正则表达式,但这里有一个扩展方法,如果你需要它可以将它添加到String:
public static class ExtensionMethods { public static String PregReplace(this String input, string[] pattern, string[] replacements) { if (replacements.Length != pattern.Length) throw new ArgumentException("Replacement and Pattern Arrays must be balanced"); for (var i = 0; i < pattern.Length; i++) { input = Regex.Replace(input, pattern[i], replacements[i]); } return input; } }
你这样使用它:
class Program { static void Main(string[] args) { String[] pattern = new String[4]; String[] replacement = new String[4]; pattern[0] = "Quick"; pattern[1] = "Fox"; pattern[2] = "Jumped"; pattern[3] = "Lazy"; replacement[0] = "Slow"; replacement[1] = "Turtle"; replacement[2] = "Crawled"; replacement[3] = "Dead"; String DemoText = "The Quick Brown Fox Jumped Over the Lazy Dog"; Console.WriteLine(DemoText.PregReplace(pattern, replacement)); } }