本文介绍了在Silverlight中删除变音符号(String.Normalize问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实创建了一个将变音符号转换为非变音符号的功能(基于此)

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

这里是代码:

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

不幸的是,由于String.Normlize不是Silverlight的一部分,因此我需要找到另一种编写此函数的方法。

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

到目前为止,我发现的解决方案是在服务器端创建一个服务,该服务将调用String.Normalize函数,然后将其返回给客户端……但这会造成巨大的性能问题。

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.

推荐答案

感谢您的回答,吉姆,但我试图像Mono项目那样实现标准化类,有一次,我意识到这是一个过大的杀手,因为对于简单的事情,涉及太多的依赖关系。

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.

我想出了一个简单的实现方法……它并不完美,我知道(这不适用于每种语言),但是它将为我完成工作MS发布了具有字符串标准化支持的Silverlight版本。

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

这篇关于在Silverlight中删除变音符号(String.Normalize问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:44