Java – Checks whether the string is Persian or English

Checks whether the string is Persian or English… here is a solution to the problem.

Checks whether the string is Persian or English

I have a webview that

will load a string from the url, I’m not sure if that’s the right way to go, but what I want to do is check if the string is in Farsi, so I changed the text of the webview to align with rtl, and if it’s English, change it to ltr. Is it possible to determine if the string is Persian or English? Or is there any other better way to deal with this?

Thanks in advance.

Solution

Try the following regular expression to check the Arabic, Farsi, and Hebrew character ranges.

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
} 

Related Problems and Solutions