How to print Arabic characters in left-to-right direction
Asked Answered
S

1

10

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?

Scabble answered 16/4, 2015 at 10:11 Comment(7)
If the "actual output" part above is correct, it seems the array is reversing its elements on its own.Northern
@SashaSalauyou please check the actual and expected outputs again. I intended to print it normally from left to right as English letters but actually it has been reversed in the output.Scabble
@GeorgeT yes it has been reversed but I don't want it to be reversed because the text is not displayed as aligned.Scabble
@Scabble i m just curious why do you need this ?Junco
Your question is very ambiguous, please add a better description of your problem -- "without having to reverse the array." Also, that is a List, not an Array.Duero
@Junco I need both arrays to be displayed according the insertion order not according to the nature of the language. So, I just need to force the output stream to print it from left to right and don't take in consideration the actual nature of the language as a right to left languageScabble
@Mr.Polywhirl actually it's an array list :), I need to reserve the insertion order in the print output and not to reverse it.Scabble
H
12

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) + "]";
}
Humphrey answered 16/4, 2015 at 10:40 Comment(5)
Would it work without a self made loop if you write System.out.println("\u200eArArray: " + ar). I haven't got a terminal that supports AR so I can't try myself.Thomasinathomasine
@Thomasinathomasine I tried to add it just before entire output, but it doesn't work. I succeeded only when adding it before each arabic character.Humphrey
Nice answer. You could also create a new class which extends ArrayList and overwrites ".toString()" with that code.Ynez
@Ynez elementData array in ArrayList is private thus cannot be accessed from child class. Of course, I can gather elements by iterator, but... It is idea for those who need this.Humphrey
Hm, I just tried it out and it works. In the overwritten toString() Method I used "this" instead of "sb.size". I still wonder though why it doesn't revert the english version (Which is good in this case).Ynez

© 2022 - 2024 — McMap. All rights reserved.