Removing diacritics in Silverlight (String.Normalize issue)

不羁岁月 提交于 2019-11-27 06:59:59

问题


I did create a function that transforms diacritic characters into non-diacritic characters (based on this post)

Here’s the code:

Public Function RemoveDiacritics(ByVal searchInString As String) As String
    Dim returnValue As String = ""

    Dim formD As String = searchInString.Normalize(System.Text.NormalizationForm.FormD)
    Dim unicodeCategory As System.Globalization.UnicodeCategory = Nothing
    Dim stringBuilder As New System.Text.StringBuilder()


    For formScan As Integer = 0 To formD.Length - 1
        unicodeCategory = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(formD(formScan))
        If unicodeCategory <> System.Globalization.UnicodeCategory.NonSpacingMark Then
            stringBuilder.Append(formD(formScan))
        End If
    Next

    returnValue = stringBuilder.ToString().Normalize(System.Text.NormalizationForm.FormC)

    Return returnValue

End Function

Unfortunately, as the String.Normlize isn’t part of Silverlight, I need to find an other way to write this function.

The only solution I have found so far is to create a service on the server side that would call the String.Normalize function and then return it to the client side… but that would create a huge performance issue.

There must be a better alternative but right know I have no clue on how to fix this problem.


回答1:


Simon,

Here is a basic implementation of Normalize(), calling into a Normalization class:

public string Normalize ()
{
    return Normalization.Normalize (this, 0);
}

public string Normalize (NormalizationForm normalizationForm)
{
    switch (normalizationForm)
    {
        default:
            return Normalization.Normalize (this, 0);
        case NormalizationForm.FormD:
            return Normalization.Normalize (this, 1);
        case NormalizationForm.FormKC:
            return Normalization.Normalize (this, 2);
        case NormalizationForm.FormKD:
            return Normalization.Normalize (this, 3);
    }
}

And you can browse an implementation of the Normalization class from the Mono project on GitHub:

http://github.com/mono/mono/blob/mono-2.6.4/mcs/class/corlib/Mono.Globalization.Unicode/Normalization.cs

Good luck,
Jim McCurdy




回答2:


Thanks for your answer Jim but I tried to implement the normalization class like the Mono Project did and at one point, I realized it was an overkill because there's way too many dependencies involved for something that should be simple.

I came up with this simple implementation... it's not perfect, I know (This won't work for every language) but it will do the job for me until MS release a version of Silverlight with string normalization support.

<System.Runtime.CompilerServices.Extension()> _    
Public Function RemoveDiacritics(ByVal searchInString As String) As String
    Dim returnValue As String = ""

    returnValue = searchInString

    returnValue = returnValue.ReplaceLowerAndUpperCase("À", "A")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Á", "A")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Â", "A")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ã", "A")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ä", "A")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Å", "A")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Æ", "A")

    returnValue = returnValue.ReplaceLowerAndUpperCase("Ç", "C")

    returnValue = returnValue.ReplaceLowerAndUpperCase("È", "E")
    returnValue = returnValue.ReplaceLowerAndUpperCase("É", "E")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ê", "E")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ë", "E")

    returnValue = returnValue.ReplaceLowerAndUpperCase("Ì", "I")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Í", "I")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Î", "I")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ï", "I")

    returnValue = returnValue.ReplaceLowerAndUpperCase("Ñ", "N")

    returnValue = returnValue.ReplaceLowerAndUpperCase("Ò", "O")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ó", "O")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ô", "O")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Õ", "O")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ö", "O")

    returnValue = returnValue.ReplaceLowerAndUpperCase("Ù", "U")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ú", "U")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Û", "U")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Ü", "U")

    returnValue = returnValue.ReplaceLowerAndUpperCase("Ý", "Y")

    returnValue = returnValue.ReplaceLowerAndUpperCase("Æ", "AE")
    returnValue = returnValue.ReplaceLowerAndUpperCase("Œ", "OE")

    Return returnValue

End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function ReplaceLowerAndUpperCase(ByVal searchInString As String, ByVal oldString As String, ByVal newString As String) As String
    Dim returnValue As String = ""

    returnValue = searchInString.Replace(oldString.ToLower, newString.ToLower)
    returnValue = returnValue.Replace(oldString.ToUpper, newString.ToUpper)

    Return returnValue
End Function


来源:https://stackoverflow.com/questions/4046937/removing-diacritics-in-silverlight-string-normalize-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!