Char.IsSymbol(“*”) is false

前端 未结 3 1814
清酒与你
清酒与你 2020-12-19 02:35

I\'m working on a password validation routine, and am surprised to find that VB does not consider \'*\' to be a symbol per the Char.IsSymbol() check. Here is the output from

3条回答
  •  臣服心动
    2020-12-19 03:38

    Characters that are symbols in this context: UnicodeCategory.MathSymbol, UnicodeCategory.CurrencySymbol, UnicodeCategory.ModifierSymbol and UnicodeCategory.OtherSymbol from the System.Globalization namespace. These are the Unicode characters designated Sm, Sc, Sk and So, respectively. All other characters return False.

    From the .Net source:

    internal static bool CheckSymbol(UnicodeCategory uc)
    {
        switch (uc)
        {
            case UnicodeCategory.MathSymbol:
            case UnicodeCategory.CurrencySymbol:
            case UnicodeCategory.ModifierSymbol:
            case UnicodeCategory.OtherSymbol:
                return true;
            default:
                return false;
        }
    }
    

    or converted to VB.Net:

    Friend Shared Function CheckSymbol(uc As UnicodeCategory) As Boolean
        Select Case uc
            Case UnicodeCategory.MathSymbol, UnicodeCategory.CurrencySymbol, UnicodeCategory.ModifierSymbol, UnicodeCategory.OtherSymbol
                Return True
            Case Else
                Return False
        End Select
    End Function
    

    CheckSymbol is called by IsSymbol with the Unicode category of the given char.

    Since the * is in the category OtherPunctuation (you can check this with char.GetUnicodeCategory()), it is not considered a symbol, and the method correctly returns False.

    To answer your question: use char.GetUnicodeCategory() to check which category the character falls in, and decide to include it or not in your own logic.

提交回复
热议问题