How to check if string only contains (A-Z 0-9)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

How to check if string only contains (A-Z 0-9)

 
0
  #1
Sep 1st, 2009
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 ?

  1. String ThisString= TextBox1.Text.Trim().ToLower();
  2. for (int i = 0; i < ThisString.Length; i++)
  3. {
  4. if( ThisString.Substring(i, 1) == ??? )
  5. {
  6. //Found a valid character so go on with code
  7. }
  8. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How to check if string only contains (A-Z 0-9)

 
0
  #2
Sep 1st, 2009
Originally Posted by Jennifer84 View Post
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 ?

  1. String ThisString= TextBox1.Text.Trim().ToLower();
  2. for (int i = 0; i < ThisString.Length; i++)
  3. {
  4. if( ThisString.Substring(i, 1) == ??? )
  5. {
  6. //Found a valid character so go on with code
  7. }
  8. }
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: How to check if string only contains (A-Z 0-9)

 
0
  #3
Sep 1st, 2009
That sounds nice. I will take a look at your suggestions and see how that could be done.

This was checking character by character:
  1. String AllChars = "abcdefghijklmnopqrstuvwxyz0123456789_";
  2. String ThisString = TextBox1.Text.Trim().ToLower();
  3. int NoValid = 0;
  4. int PartValid = 0;
  5. for (int u = 0; u < ThisString.Length; u++)
  6. {
  7. PartValid = 0; //Reset
  8. for (int q = 0; q < AllChars.Length; q++)
  9. {
  10. if (ThisString.Substring(u, 1) == AllChars.Substring(q, 1))
  11. {
  12. PartValid = 1;
  13. }
  14. }
  15. if (PartValid == 0)//Found an invalid character;
  16. {
  17. NoValid = 1;
  18. break;
  19. }
  20. }
  21.  
  22. if (NoValid == 1)
  23. {
  24. //Found invalid character
  25. }
  26.  
  27. if (NoValid == 0)
  28. {
  29. //Valid
  30. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to check if string only contains (A-Z 0-9)

 
0
  #4
Sep 1st, 2009
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:
Originally Posted by VernonDozier View Post
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.
Last edited by Dave Sinkula; Sep 1st, 2009 at 5:54 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC