Frank Thomas
Frank Thomas

Reputation: 2514

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?

Upvotes: 10

Views: 5605

Answers (4)

HobPet
HobPet

Reputation: 662

IsPunctuation(x) is what you are looking for.

This worked for me in C#:

string Password = "";
ConsoleKeyInfo key;

do
{
    key = Console.ReadKey(true);
    // Ignore any key out of range.
   if (char.IsPunctuation(key.KeyChar) ||char.IsLetterOrDigit(key.KeyChar) || char.IsSymbol(key.KeyChar))
    {
        // Append the character to the password.
        Password += key.KeyChar;
        Console.Write("*");
    }
    // Exit if Enter key is pressed.
} while (key.Key != ConsoleKey.Enter);

Upvotes: 0

PavelB
PavelB

Reputation: 71

If you simply need to know that character is something else than digit or letter, use just

!char.IsLetterOrDigit(c) 

preferably with

&& !char.IsControl(c)

Upvotes: 7

John Willemse
John Willemse

Reputation: 6698

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.

Upvotes: 9

gpinkas
gpinkas

Reputation: 2591

Maybe you have the compiler option "strict" of, because with

Char.IsSymbol("*")

I get a compiler error

BC30512: Option Strict On disallows implicit conversions from 'String' to 'Char'.

To define a Character literal in VB.NET, you must add a c to the string, like this:

Char.IsSymbol("*"c)

Upvotes: 0

Related Questions