How to determine if a string contains any character from right to left?

I am trying to create a method to detect strings written in languages ​​from right to left in Java. I came up with this question to do something like this in C #.
Now I need something similar, but written in Java.
Any help is appreciated.

+6
source share
4 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; } } 

This is not a very effective or, for that matter, exact method, but it can give one idea.

+10
source

The question is old, but maybe someone may have the same problem ...

After trying a few solutions, I found 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 } 
+7
source

Here's an 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 
+1
source

Perhaps this should help:

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

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

Hi

0
source

Source: https://habr.com/ru/post/920220/


All Articles