How do I remove diacritics (accents) from a string in .NET?

前端 未结 20 2849
南方客
南方客 2020-11-21 05:44

I\'m trying to convert some strings that are in French Canadian and basically, I\'d like to be able to take out the French accent marks in the letters while keeping the lett

20条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 06:08

    I really like the concise and functional code provided by azrafe7. So, I have changed it a little bit to convert it to an extension method:

    public static class StringExtensions
    {
        public static string RemoveDiacritics(this string text)
        {
            const string SINGLEBYTE_LATIN_ASCII_ENCODING = "ISO-8859-8";
    
            if (string.IsNullOrEmpty(text))
            {
                return string.Empty;
            }
    
            return Encoding.ASCII.GetString(
                Encoding.GetEncoding(SINGLEBYTE_LATIN_ASCII_ENCODING).GetBytes(text));
        }
    }
    

提交回复
热议问题