Regular Expression replace specific characters Does anyone know of a better / cleaner way to write the following:

GetSafeTagName(txtUserInput.text);

public static string GetSafeTagName(string tag)
{
    tag = tag.ToUpper()
    .Replace("'","`")
    .Replace('"','`')
    .Replace("&amp;", "and")
    .Replace(",",":")
    .Replace(@"\","/"); //Do not allow escaped characters from user
    tag = Regex.Replace(tag, @"\s+", " "); //multiple spaces with single spaces
    return tag;
}


Thanks!
You could use a regular expression and Dictionary&lt;string, string&gt; to do the search replace:

// This regex matches either one of the special characters, or a sequence of 
// more than one whitespace characters.
Regex regex = new Regex("['\"&amp;,\\\\]|\\s{2,}");

var map = new Dictionary&lt;string, string&gt; {
    { "'", "`"},
    { "\"", "`"},
    { "&amp;", "and" },
    { ",", ":" },
    { "\\", "/" }
};

// If the length of the match is greater that 1, then it's a sequence
// of spaces, and we can replace it by a single space. Otherwise, we
// use the dictionary to map the character.
string output = regex.Replace(input.ToUpper(), 
    m =&gt; m.Value.Length &gt; 1 ? " " : map[m.Value]);