I think your method is pretty readable and clean as it stands. However, if you want to simplify the method itself, I have a decent support structure that would do so and possibly speed it up for particularly long strings:
internal static partial class SafeTag
{
private static readonly List<Replacement>IEnumerable<Replacement> replacements_Replacements = new List<Replacement>
{[
new Replacement("'", "`"),
new Replacement("\"", "`"),
new Replacement("&", "and"),
new Replacement(",", ":"),
new Replacement("\\", "/"),
};];
private static readonly Regex spaces_Spaces = new RegexSpaces("\\s+", RegexOptions.Compiled);
public static string GetSafeTagName(this string tag)
{
varStringBuilder parse = new StringBuilder(tag.ToUpper());
replacementsparse = _Replacements.ForEachAggregate(replacement
=> parse = parse,
(current, replacement) => current.Replace(replacement.Original, replacement.ToReplaceWith));
return spaces_Spaces.Replace(parse.ToString(), " ");
}
private struct Replacement
{
private readonly string original;
private readonly string toReplaceWith;
internalstruct Replacement(string original, string toReplaceWith)
{
this.original = original;
this.toReplaceWith = toReplaceWith;
}
internalpublic string Original
{
get
{
return this.original;
get; }
= }original;
internalpublic string ToReplaceWith
{
get; } = gettoReplaceWith;
{}
return this.toReplaceWith;
[GeneratedRegex("\\s+", }RegexOptions.Compiled)]
}
private static partial Regex }Spaces();
}