Logan Slowik Logan Slowik - 1 year ago 99
C# Question

What exactly is classified as a "symbol" in C#?

So I am trying to write a program that asks for you to create a password. I have a block of code that checks to see if the string entered by the user contains a symbol. I have the code set to exit the loop when the boolean value 'validPassword' equals true.

string pleaseenterapassword = "Create a password:";
bool validPassword = false;
Console.WriteLine(pleaseenterapassword); // Writes to the screen "Create a password:"
string password = Console.ReadLine(); //Sets the text entered in the Console into the string 'password'
bool containsAtLeastOneSymbol = password.Any(char.IsSymbol);
if (containsAtLeastOneSymbol == false) // Checks if your password contains at least one symbol
Console.WriteLine("Your password must contain at least one symbol.");
validPassword = false;

This code is successful if I enter something like "Thisismypassword905+", but it does not work if I enter something like "Thisismypassword95*". I would appreciate any sort of help. Thanks in advance!

Answer Source

From msdn:

Valid symbols are members of the following categories in UnicodeCategory: MathSymbol, CurrencySymbol, ModifierSymbol, and OtherSymbol. Symbols in the Unicode standard are a loosely defined set of characters that include the following:

  • Currency symbols.
  • Letterlike symbols, which include a set of mathematical alphanumeric symbols as well as symbols such as ℅, №, and ™.
  • Number forms, such as subscripts and superscripts.
  • Mathematical operators and arrows
  • Geometric symbols.
  • Technical symbols.
  • Braille patterns.

  • Dingbats

Update: For juharr, who asked why does the '*' is not in the category of MathSymbol. The asterisk is not in the category of MathSymbols with this snippet you can check it. Or see this fiddle

      int ctr = 0;
      UnicodeCategory category = UnicodeCategory.MathSymbol;

      for (ushort codePoint = 0; codePoint < ushort.MaxValue; codePoint++) {
         Char ch = Convert.ToChar(codePoint);

         if (CharUnicodeInfo.GetUnicodeCategory(ch) == category) {
            if (ctr % 5 == 0)
            Console.Write("{0} (U+{1:X4})     ", ch, codePoint);
      Console.WriteLine("\n{0} characters are in the {1:G} category", 
                        ctr, category);   
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download