Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a string contains any Right-to-Left character?

I'm trying to make a method to detect strings written in right to left languages in Java. I've come up with this question doing something similar in C#.
Now I need to have something like that but written in Java.
Any help is appreciated.

like image 307
2hamed Avatar asked Jul 11 '12 13:07

2hamed


People also ask

How do you check if a string contains a certain character?

The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement.


1 Answers

I came up with the following code:

char[] chars = s.toCharArray();
for(char c: chars){
    if(c >= 0x600 && c <= 0x6ff){
        //Text contains RTL character
        break;
     }
}

It's not a very efficient or for that matter an accurate way but can give one ideas.

like image 183
2hamed Avatar answered Nov 15 '22 18:11

2hamed