A super string cleaner I need a method that clean up a string considering these few rules:


Remove or not Spaces
Remove or not Special Chars
Remove or not Numbers
Descaptalize or not the string (this is don't really needed but I included it anyway). 


I did some google if exists something that already do this, but found nothing, so I came up with this:

public static class StringHelper
{
    private static readonly Dictionary&lt;char, char&gt; _specialChars
        = new Dictionary&lt;char, char&gt;
    {
        { 'á', 'a' },{ 'à', 'a' },{ 'â', 'a' },{ 'ã', 'a' },{ 'ä', 'a' },
        { 'é', 'e' },{ 'è', 'e' },{ 'ê', 'e' },{ 'ë', 'e' },
        { 'í', 'i' },{ 'ì', 'i' },{ 'î', 'i' },{ 'ï', 'i' },
        { 'ó', 'o' },{ 'ò', 'o' },{ 'õ', 'o' },{ 'ö', 'o' },
        { 'ú', 'u' },{ 'ù', 'u' },{ 'û', 'u' },{ 'ü', 'u' },
        { 'ç', 'c' },{ 'ñ', 'n' }
    };

    static StringHelper()
    {
        foreach (var item in _specialChars.ToList())
        {
            _specialChars.Add(
                item.Key.ToString().ToUpper()[0],
                item.Value.ToString().ToUpper()[0]);
        }
    }

    private static readonly Regex _spaceRegex = new Regex(@"\s", RegexOptions.Compiled);
    private static readonly Regex _numericRegex = new Regex(@"\s", RegexOptions.Compiled);

    public static string Normalize(this string str, CharType normaliar = CharType.All)
    {
        if (normaliar.HasFlag(CharType.Spaces))
        {
            str = _spaceRegex.Replace(str, string.Empty);
        }

        if (normaliar.HasFlag(CharType.Special))
        {
            str = _specialChars.ToList()
                .Aggregate(str, (ac, item) =&gt; ac.Replace(item.Key, item.Value));
        }

        if (normaliar.HasFlag(CharType.Numerics))
        {
            str = _numericRegex.Replace(str, string.Empty);
        }

        if (normaliar.HasFlag(CharType.UpperCase))
        {
            str = str.ToLower();
        }

        return str;
    }
}

public enum CharType
{
    Spaces = 1,
    Special = 2,
    Numerics = 4,
    UpperCase = 8,
    All = 15,
}


I'm not considering really all special char just Portuguese, Spanish and a few more.

As far as I could, I wrote with the better performance and organization, but this was my limit.
private static readonly Regex _numericRegex = new Regex(@"\s", RegexOptions.Compiled);



That should be \d, not \s.



Instead of


_specialChars.Add(
    item.Key.ToString().ToUpper()[0],
    item.Value.ToString().ToUpper()[0]);



You can write

_specialChars.Add(
    Char.ToUpperInvariant(item.Key),
    Char.ToUpperInvariant(item.Value));


I find it a bit cleaner, and there is less work involved.



If the text is potentially very long, you may be better off doing just one pass of the string. You would need to profile against the sort of data you have, but it could look something like this

var spaces = normaliar.HasFlag(CharType.Spaces);
var numerics = normaliar.HasFlag(CharType.Numerics);
var uppercase = normaliar.HasFlag(CharType.UpperCase);
var special = normaliar.HasFlag(CharType.Special);

var sb = new StringBuilder(str.Length);

for (var i = 0; i &lt; str.Length; i++)
{
    var c = str[i];
    if (spaces &amp;&amp; Char.IsWhiteSpace(c))
    {
        continue;
    }

    if (numerics &amp;&amp; Char.IsNumber(c))
    {
        continue;
    }

    if (uppercase)
    {
        c = Char.ToLowerInvariant(c);
    }

    if (special)
    {
        char replacement;
        if (_specialChars.TryGetValue(c, out replacement))
        {
            c = replacement;
        }
    }

    sb.Append(c);
}

return sb.ToString();