Search code examples
javaandroidstringandroid-webviewpersian

check if string is persian or english


I have a webview which will load a string from a url, I'm not sure if this is the correct way or not but what I want to do is to check if the string is in persian so I change my webview's text alignment to rtl and else if it's in english change it to ltr. Is it possible to determine if the string is in persian or english? or if there's any other better way to handle this matter ?

Thanks in advance.


Solution

  • Try the following regular expression, to check the Arabic, Persian and Hebrew characters range.

    public static final Pattern RTL_CHARACTERS = 
        Pattern.compile("[\u0600-\u06FF\u0750-\u077F\u0590-\u05FF\uFE70-\uFEFF]");
    Matcher matcher = RTL_CHARACTERS.matcher("براي تست");
    if(matcher.find()){
       return true;  // it's RTL
    }