确定一个角色是否是变音符号?

时间:2014-03-11 19:02:44

标签: .net vb.net character-encoding character diacritics

我使用下一个函数来转换变音字符,然后获得该字符的键码,但在调用此方法之前,我首先需要知道该字符是否是变音符号,不要进行冗余呼吁这种方法应该区分变音符号和非变音符号。

那么,如何判断一个字符是否是变音符号?

PS:查看代码中标记的共识。

这个想法是,对于角色O,该方法应返回79,对于角色Ó,该方法将删除变音符号,因此我得到O然后我用O再次调用该函数,返回另一个79,但是如果在keybouardlayout上找不到该字符,该方法将尝试删除变音符号,即使该字符不是变音符号,并且一直调用相同的函数,所以我需要确定该字符是否是变音符号。

Public Shared Function GetKeyCode(ByVal Character As Char,
                                  Optional ByVal KeyboardLayout As IntPtr = Nothing) As Short

    ' Get the Keycode of the character.
    Dim Keycode As Short =
        BitConverter.GetBytes(VkKeyScanEx(Character)).First

    Select Case Keycode

        Case Is <> 255 ' Character is found on the current KeyboardLayout.
            Return Keycode

        Case Else ' Character is not found on the current layour (Maybe is a diacritic character?)

            ' ****************************************************************************
            ' I want to perform the instructions below only if the character is diacritic.
            ' ****************************************************************************

            Dim s As String = CStr(Character).Normalize(System.Text.NormalizationForm.FormKD)

            For Each c As Char In s

                Select Case Globalization.CharUnicodeInfo.GetUnicodeCategory(c)

                    Case Globalization.UnicodeCategory.NonSpacingMark,
                         Globalization.UnicodeCategory.SpacingCombiningMark,
                         Globalization.UnicodeCategory.EnclosingMark

                        ' Do nothing.
                        Exit Select

                    Case Else ' Character is diacritic so we remove the diacritic and try to return the Keycode.
                        Return GetKeyCode(c, KeyboardLayout)

                End Select

            Next c

            ' ****************************************************************************
            ' I want to perform the instructions above only if the character is diacritic.
            ' ****************************************************************************

            Return 255 ' Character is not diacritic and the keycode can't be found.

    End Select

1 个答案:

答案 0 :(得分:1)

知道章程是否是变音符号的安全赌注是测试它。

一种选择是通过所有Unicode一次并将变音符号放在HashSet中。

如果您正在测试一个长字符串,那么将整个sting标准化一次。

如果您想要更广泛的映射,请考虑编码为win1252。