Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char.IsSymbol("*") is false

Tags:

vb.net

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?

like image 736
Frank Thomas Avatar asked Feb 05 '13 13:02

Frank Thomas


People also ask

What is Char IsSymbol?

IsSymbol(Char) Indicates whether the specified Unicode character is categorized as a symbol character. IsSymbol(String, Int32) Indicates whether the character at the specified position in a specified string is categorized as a symbol character.

How to check if a Char is a symbol c#?

IsSymbol(String, Int32) Method. This method is used to check whether a character in the specified string at the specified position is a valid symbol or not. If it is a symbol according to the Unicode standard then it returns True otherwise returns False.


1 Answers

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.

like image 194
John Willemse Avatar answered Oct 18 '22 21:10

John Willemse