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.