如何从左到右的方向打印阿拉伯字符

时间:2015-04-16 10:11:23

标签: java unicode left-to-right

我有一系列英文和阿拉伯文字,应该以对齐方式打印。

例如:

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);   

预期输出:

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

实际输出:

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

有没有办法在从左到右的方向打印阿拉伯字符而不在输出前明确反转列表?

1 个答案:

答案 0 :(得分:10)

您需要在每个RTL字符前添​​加left-to-right mark '\u200e'以使其打印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) + "]";
}