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?
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) + "]";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With