Contents

Remove diacritics (accents) in strings

In an elegany way :P

Quick note

How to remove diacritics (accents) from a C# string?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public static string RemoveDiacritics(this string input)
{
    var stFormD = input.Normalize(NormalizationForm.FormD);
    var len = stFormD.Length;
    var sb = new StringBuilder();
    for (int i = 0; i < len; i++)
    {
        var uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[i]);
        if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
        {
            sb.Append(stFormD[i]);
        }
    }
    return (sb.ToString().Normalize(NormalizationForm.FormC));
}

Bonus track

How to remove in TSQL (Microsoft SQL Server)

1
SELECT 'àéêöhello!' Collate SQL_Latin1_General_CP1253_CI_AI

See you soon! 😉

comments powered by Disqus