I am doing a fuction where I want to check that a String contains only Letters between: "A-Z" and Numbers between: "0-9" and "_"

Instead of checking all one by one. Is there any approach to check against all at the same time ?

String ThisString= TextBox1.Text.Trim().ToLower();
            for (int i = 0; i < ThisString.Length; i++)
            {
                if( ThisString.Substring(i, 1) ==  ??? )
                 {
                      //Found a valid character so go on with code
                 }
            }

I am doing a fuction where I want to check that a String contains only Letters between: "A-Z" and Numbers between: "0-9" and "_"

Instead of checking all one by one. Is there any approach to check against all at the same time ?

String ThisString= TextBox1.Text.Trim().ToLower();
            for (int i = 0; i < ThisString.Length; i++)
            {
                if( ThisString.Substring(i, 1) ==  ??? )
                 {
                      //Found a valid character so go on with code
                 }
            }

As far as I know you cannot check an entire string at once. You have to do it a character at a time. I could be wrong, but I don't think so.

By "checking all" and "checking one by one", if you mean check to see if a character is an 'A', then check to see if it is a 'B', then a 'C', then a 'D', etc., you definitely don't need to do that.

cctype comes in handy here.

http://www.cplusplus.com/reference/clibrary/cctype/

Everything is done by single characters, but it has functions like isdigit, isalnum, isalpha, isupper that can help here, so you can check whether a character is a digit with isdigit and whether it's an upper case letter using isupper, then check for '_'. So that's three checks per character.

Can't think of any way to do it faster.

That sounds nice. I will take a look at your suggestions and see how that could be done.

This was checking character by character:

String AllChars = "abcdefghijklmnopqrstuvwxyz0123456789_";
            String ThisString = TextBox1.Text.Trim().ToLower();
            int NoValid = 0;
            int PartValid = 0;
            for (int u = 0; u < ThisString.Length; u++)
            {
                PartValid = 0; //Reset
                for (int q = 0; q < AllChars.Length; q++)
                {
                    if (ThisString.Substring(u, 1) == AllChars.Substring(q, 1))
                    {
                        PartValid = 1;
                    }
                }
                if (PartValid == 0)//Found an invalid character;
                {
                    NoValid = 1; 
                    break;
                }
            }

            if (NoValid == 1)
            {
               //Found invalid character
            }

            if (NoValid == 0)
            {
               //Valid
            }

Standard C and C++ have isalnum .

[edit]Sometimes the is* functions are implemented as lookup tables. Consider that type of approach (lookup table) yourself instead of the inner loop if speed is paramount.

[edit=2]Geez I need a nap:

cctype comes in handy here.

http://www.cplusplus.com/reference/clibrary/cctype/

Everything is done by single characters, but it has functions like isdigit, isalnum, isalpha, isupper that can help here, so you can check whether a character is a digit with isdigit and whether it's an upper case letter using isupper, then check for '_'. So that's three checks per character.

:P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.