I'd think a regex is going to be kind of brittle for this kind of thing. If your version of .NET has extension methods and you'd like a cleaner syntax that scales you might introduce an extension method like this:
public static class StringExtensions
{
public static string ReplaceMany(this string s, Dictionary<string, string> replacements)
{
var sb = new StringBuilder(s);
foreach (var replacement in replacements)
{
sb = sb.Replace(replacement.Key, replacement.Value);
}
return sb.ToString();
}
}
So now you build up your dictionary of replacements...
var replacements = new Dictionary<string, string> { {"(", "-"}, {")", ""} };
And call ReplaceMany:
var result = "foobar(baz2)".ReplaceMany(replacements); // result = foobar-baz2
If you really want to show your intent you can alias Dictionary<string,string>
to StringReplacements
:
//At the top
using StringReplacements = System.Collections.Generic.Dictionary<string,string>;
//In your function
var replacements = new StringReplacements() { {"(", "-"}, {")", ""} };
var result = "foobar(baz2)".ReplaceMany(replacements);
Might be overkill for only two replacements, but if you have many to make it'll be cleaner than .Replace().Replace().Replace().Replace()...
.