我确实创建了一个将变音符号字符转换为非变音符号字符的函数(基于此 邮政)

代码如下:

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 的一部分,我需要找到其他方法来编写此函数。

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

一定有更好的选择,但我知道我不知道如何解决这个问题。

有帮助吗?

解决方案 2

感谢您的回答吉姆,但我尝试像 Mono 项目那样实现规范化类,并且在某一时刻,我意识到这是一种矫枉过正,因为对于应该简单的事情涉及太多依赖项。

我想出了这个简单的实现......我知道它并不完美(这并不适用于每种语言),但它会为我完成这项工作,直到 MS 发布支持字符串规范化的 Silverlight 版本。

<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

其他提示

西蒙,

下面是 Normalize() 的基本实现,调用 Normalization 类:

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);
    }
}

您可以从 GitHub 上的 Mono 项目浏览 Normalization 类的实现:

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

祝你好运,
吉姆·麦柯迪

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top