11

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.

4 Answers 4

13

Question is old but maybe someone else might have the same problem...

After trying several solutions I found the one that works for me:

if (Character.getDirectionality(string.charAt(0)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT
    || Character.getDirectionality(string.charAt(0)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC
    || Character.getDirectionality(string.charAt(0)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING
    || Character.getDirectionality(string.charAt(0)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE
    ) {

    // it is a RTL string
}
2
  • This wouldn't work, as RTL text is written without these marks.
    – Liggliluff
    Feb 9, 2017 at 23:43
  • 2
    @Liggliluff there is no mark, the detection is used directly on chars, Character.getDirectionality(char)
    – cdalxndr
    Jul 29, 2021 at 17:06
13

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.

1
  • 15
    You should use (c >= 0x5D0 && c <= 0x6ff) to include Hebrew, which is also an RTL language.
    – Arik
    Feb 25, 2014 at 8:51
9

Here's improved version of Darko's answer:

public static boolean isRtl(String string) {
    if (string == null) {
        return false;
    }

    for (int i = 0, n = string.length(); i < n; ++i) {
        byte d = Character.getDirectionality(string.charAt(i));

        switch (d) {
            case DIRECTIONALITY_RIGHT_TO_LEFT:
            case DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
            case DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING:
            case DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE:
                return true;

            case DIRECTIONALITY_LEFT_TO_RIGHT:
            case DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING:
            case DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE:
                return false;
        }
    }

    return false;
}

This code works for me for all of the following cases:

בוקר טוב               => true
good morning בוקר טוב  => false
בוקר טוב good morning  => true
good בוקר טוב morning  => false
בוקר good morning טוב  => true
(בוקר טוב)             => true
0

Maybe this should help:

http://en.wikipedia.org/wiki/Right-to-left_mark

There should be a Unicode char, namely U+200F, when a rtl string is present.

Regards

1
  • "\u05D0\u05D2" is displayed inverse, without any RTL mark.
    – cdalxndr
    Jul 29, 2021 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.