Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Arabic characters in left-to-right direction

I have a sequence of English and Arabic text that should be printed in an aligned way.

For example:

List<Character> ar = new ArrayList<Character>();
ar.add('ا');
ar.add('ب');
ar.add('ت');

List<Character> en = new ArrayList<Character>();
en.add('a');
en.add('b');
en.add('c');

System.out.println("ArArray: " + ar);
System.out.println("EnArray: " + en);   

Expected Output:

ArArray: [ت, ب, ا] // <- I want characters to be printed in the order they were added to the list
EnArray: [a, b, c]

Actual Output:

ArArray: [ا, ب, ت] // <- but they're printed in reverse order
EnArray: [a, b, c]

Is there a way to print Arabic characters in left-to-right direction without explicitly reversing the list before output?

like image 231
vanilla Avatar asked Apr 16 '15 10:04

vanilla


1 Answers

You need to add the left-to-right mark '\u200e' before each RTL character to make it be printed LTR:

public String printListLtr(List<Character> sb) {
    if (sb.size() == 0) 
        return "[]";
    StringBuilder b = new StringBuilder('[');
    for (Character c : sb) {
        b.append('\u200e').append(c).append(',').append(' '); 
    }
    return b.substring(0, b.length() - 2) + "]";
}
like image 116
Alex Salauyou Avatar answered Oct 02 '22 04:10

Alex Salauyou