The easiest way to check the base latin letter (AZ)

Maybe I am missing something obvious, but is there an easier way to check if a character is a base Latin letter (az) other than converting to a string using Regex ?: For example:

public static bool IsBasicLetter(Char c) { return Regex.IsMatch(c.ToString(), "[az]", RegexOptions.IgnoreCase); } 

Char.IsLetter matches hundreds of letter characters from many alphabets. I could check the codes directly, but this looks sketchy:

 public static bool IsBasicLetter(Char c) { int cInt = c; return !(cInt < 65 || cInt > 122 || (cInt > 90 & cInt < 97)); } 
+4
source share
3 answers

The second bit of code looks much better if you use character literals:

 public static bool IsBasicLetter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } 
+13
source

How about this?

 return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'); 
+2
source

If you want to make this a little more complicated, for example, a subset of AZ and several other characters (for example, to check for valid Base64 characters), consider using an array of boolean flags for the first 127 or 255 characters with true for the characters you want to allow in the IsBasicLetter method . Thus, most libraries implement this type of function.

For more Unicode-friendly character validation methods (clearly beyond the scope of the question), check out Char class methods such as IsLetterOrDigit , which checks all possible letter variants.

+2
source

Source: https://habr.com/ru/post/1394014/


All Articles