Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to check for basic latin letter (A-Z)

Maybe I'm missing something obvious, but is there a simpler way to check if a character is a basic latin letter (a-z) other than converting to a string and using Regex?: For example:

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

Char.IsLetter matches hundreds of letter characters from many alphabets. I could directly check the code points, but that feels sketchy:

public static bool IsBasicLetter(Char c) {
    int cInt = c;
    return !(cInt < 65 || cInt > 122 || (cInt > 90 & cInt < 97));
}
like image 803
Joshua Honig Avatar asked Jan 31 '12 16:01

Joshua Honig


People also ask

How do I check if a string contains Latin characters?

To check if a string contains only latin characters, use the test() method with the following regular expression /^[a-zA-Z]+$/ . The test method will return true if the string contains only latin characters and false otherwise.

What is the basic of Latin alphabet?

The ISO basic Latin alphabet is composed of 26 uppercase letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ) and 26 lowercase versions of the same letters (abcdefghijklmnopqrstuvwxyz), resulting in the following pairs: Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz.

What's a lowercase Latin letter?

There were no lowercase letters. ABCDEFGHIJKLM. NOPQRSTUVWXYZ. For phonetic reasons, the symbols “J”, “U” and “W” were added to our alphabet during the Middle Ages. The Latin language used an “I” symbol where we use a “J”, a “V” symbol where we use a “U”.

How do you write Latin letters?

Classic Latin is written using the same alphabet as English. There are many other languages that use this alphabet, including Spanish, Italian, and French—although other languages use accent marks that aren't used for English or Latin. In practice, some letters aren't used to spell Latin words, such as "j" and "k."


2 Answers

Your second bit of code looks a lot better if you use character literals:

public static bool IsBasicLetter(char c) {
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
like image 175
Jon Skeet Avatar answered Sep 28 '22 02:09

Jon Skeet


How about this?

return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
like image 29
Botz3000 Avatar answered Sep 28 '22 01:09

Botz3000