10

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

例如:

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]

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

4

1 回答 1

11

您需要在每个 RTL 字符之前添加从左到右的标记'\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) + "]";
}
于 2015-04-16T10:40:04.127 回答