| | |
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:
Solved Threads: 1
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 ?
Instead of checking all one by one. Is there any approach to check against all at the same time ?
C++ Syntax (Toggle Plain Text)
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 } }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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 ?
C++ Syntax (Toggle Plain Text)
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 } }
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.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
That sounds nice. I will take a look at your suggestions and see how that could be done.
This was checking character by character:
This was checking character by character:
C++ Syntax (Toggle Plain Text)
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
[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:
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.
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
![]() |
Similar Threads
- Check a string is numeric or not (Java)
- urgent....need to make fragments of string (C++)
- Determining Invalid Characters in a String?? (C)
- Error Check (Visual Basic 4 / 5 / 6)
- check whether a palindrome can be formed from a given stirng (Java)
- Validation Check (Java)
- String Match Comparison (Shell Scripting)
- Can anyone check my code!!!! (C)
- Check string value (JSP)
Other Threads in the C++ Forum
- Previous Thread: Massive overhauld needed on fundamentals
- Next Thread: Square root in C++
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






