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

前端 未结 20 2843
南方客
南方客 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 05:59

    This is how i replace diacritic characters to non-diacritic ones in all my .NET program

    C#:

    //Transforms the culture of a letter to its equivalent representation in the 0-127 ascii table, such as the letter 'é' is substituted by an 'e'
    public string RemoveDiacritics(string s)
    {
        string normalizedString = null;
        StringBuilder stringBuilder = new StringBuilder();
        normalizedString = s.Normalize(NormalizationForm.FormD);
        int i = 0;
        char c = '\0';
    
        for (i = 0; i <= normalizedString.Length - 1; i++)
        {
            c = normalizedString[i];
            if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
            {
                stringBuilder.Append(c);
            }
        }
    
        return stringBuilder.ToString().ToLower();
    }
    

    VB .NET:

    'Transforms the culture of a letter to its equivalent representation in the 0-127 ascii table, such as the letter "é" is substituted by an "e"'
    Public Function RemoveDiacritics(ByVal s As String) As String
        Dim normalizedString As String
        Dim stringBuilder As New StringBuilder
        normalizedString = s.Normalize(NormalizationForm.FormD)
        Dim i As Integer
        Dim c As Char
    
        For i = 0 To normalizedString.Length - 1
            c = normalizedString(i)
            If CharUnicodeInfo.GetUnicodeCategory(c) <> UnicodeCategory.NonSpacingMark Then
                stringBuilder.Append(c)
            End If
        Next
        Return stringBuilder.ToString().ToLower()
    End Function
    

提交回复
热议问题