Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vb.net - Char.IsSymbol("*") is false

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 the QuickWatch:

char.IsSymbol("*")  False   Boolean

The MS documentation does not specify what characters are matched by IsSymbol, but does imply that standard mathematical symbols are included here.

Does anyone have any good ideas for matching all standard US special characters?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...