So I want to check that a string has all these attributes:
- Five characters long, and
- The first character is one of:
- O, or
- S, or
- J, or
- C, and
- The last four characters are digits.
This is my code:
Console.Write("IDnumber : ");
IDnumber= Console.ReadLine();
IDnumberLength = IDnumber.Length;
if (MemberNumber.Length == 5 &&
char.IsLetter(IDnumber[0]) && <-- I know how to validate any letter but not certain letter
char.IsDigit(IDnumber[1]) &&
char.IsDigit(IDnumber[2]) &&
char.IsDigit(IDnumber[3]) &&
char.IsDigit(IDnumber[4]))
[OSJC][0-9]{4}would fit. Alternatively, your code plusnew char[] { 'O', 'S', 'J', 'C' }.Contains(IDnumber[0]).char.Ismethods also check for Unicode characters fileformat.info/info/unicode/category/Nd/list.htm, soIDnumber[1] >= '0' && IDnumber[1] <= '9'might be preferred