Advertisements
I would use Char.IsLetterOrDigit but I need the letters to be uppercase only.
I can’t seem to find a way to combine Char.IsLetterOrDigit and Char.IsUpper because digits won’t be allowed when I try this way:
if (input.All(Char.IsLetterOrDigit) && input.All(Char.IsUpper))
Am I missing something here?
How can I check each letter separately?
>Solution :
All()
takes a predicate which can be a lambda expression. You can use this to check IsLetter && IsUpper
separately from IsDigit
.
input.All(c => (Char.IsLetter(c) && Char.IsUpper(c)) || Char.IsDigit(c))
Alternatively, you could use a regular expression that matches zero or more uppercase characters or digits:
using System.Text.RegularExpressions;
Regex r = new Regex(@"^[A-Z0-9]*$");
r.IsMatch(input);
Regex explanation:
^[A-Z0-9]*$
^ Start of string
[A-Z0-9] The uppercase letter characters or the digit characters
* Zero or more of the previous
$ End of string