How do I verify that a string is in English?

One other way is to check if IsLower and IsUpper both doesn't return true. Something like :

    private bool IsAllCharEnglish(string Input)
    {
        foreach (var item in Input.ToCharArray())
        {
            if (!char.IsLower(item) && !char.IsUpper(item) && !char.IsDigit(item) && !char.IsWhiteSpace(item))
            {
                return false;
            }
        }
        return true;
    }

and for use it :

        string str = "فارسی abc";
        IsAllCharEnglish(str); // return false
        str = "These are english 123";
        IsAllCharEnglish(str); // return true

You could match it against this regular expression: ^[a-zA-Z0-9]*$

  • ^ matches the start of the string (ie no characters are allowed before this point)
  • [a-zA-Z0-9] matches any letter from a-z in lower or upper case, as well as digits 0-9
  • * lets the previous match repeat zero or more times
  • $ matches the end of the string (ie no characters are allowed after this point)

To use the expression in a C# program, you will need to import System.Text.RegularExpressions and do something like this in your code:

bool match = Regex.IsMatch(input, "^[a-zA-Z0-9]*$");

If you are going to test a lot of lines against the pattern, you might want to compile the expression:

Regex pattern = new Regex("^[a-zA-Z0-9]*$", RegexOptions.Compiled);

for (int i = 0; i < 1000; i++)
{
    string input = Console.ReadLine();
    pattern.IsMatch(input);
}

Assuming that by "English characters" you are simply referring to the 26-character Latin alphabet, this would be an area where I would use regular expressions: ^[a-zA-Z0-9 ]*$

For example:

if( Regex.IsMatch(Console.ReadLine(), "^[a-zA-Z0-9]*$") )
{ /* your code */ }

The benefit of regular expressions in this case is that all you really care about is whether or not a string matches a pattern - this is one where regular expressions work wonderfully. It clearly captures your intent, and it's easy to extend if you definition of "English characters" expands beyond just the 26 alphabetic ones.

There's a decent series of articles here that teach more about regular expressions.

Jørn Schou-Rode's answer provides a great explanation of how the regular expression presented here works to match your input.


The accepted answer does not work for the white spaces or punctuation. Below code is tested for this input:

Hello: 1. - a; b/c \ _(5)??
(Is English)

Regex regex = new Regex("^[a-zA-Z0-9. -_?]*$");


string text1 = "سلام";
bool fls = regex.IsMatch(text1);   //false

string text2 = "123 abc! ?? -_)(/\\;:";
bool tru = regex.IsMatch(text2);  //true