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) + "]";
}
来源:https://stackoverflow.com/questions/29671593/how-to-print-arabic-characters-in-left-to-right-direction