I have a string. I need to replace all instances of a given array of strings from this original string – how would I do that?
Currently I am using…
var inputString = 'this is my original string.'; var replacement = ''; var pattern = string.Join('|', arrayOfStringsToRemove); Regex.Replace(inputString, pattern, replacement);
This works fine, but unfortunately it breaks down when someone tries to remove a character that has a special meaning in the regex.
How should I do this? Is there a better way?
Build the pattern using Regex.Escape: