How to determine a string is English or Persian?

All possible Unicode ranges for Persian (also for Urdu) alphabet:

  • 0x0600 to 0x06FF

  • 0xFB50 to 0xFDFF

  • 0xFE70 to 0xFEFF

    So if you want don't miss any char check all ranges. Hope helps you.


Why don't you evaluate it when keyboard is popup.. Means You can do it by getting the language of phone... here is the method useLocale.getDefault().getDisplayLanguage(); minSDK is 11 is required.


You can know a string is english or persian by using Regex.

public static final Pattern VALID_NAME_PATTERN_REGEX = Pattern.compile("[a-zA-Z_0-9]+$");

public static boolean isEnglishWord(String string) {
    return VALID_NAME_PATTERN_REGEX.matcher(string).find();
}

this only works with words and numbers. if there is a character like '=' or '+' , the function would return false . you can fix that by editing the regex to match what you need .

Tags:

Java

Android