如何从左到右打印阿拉伯字符
How to print Arabic characters in left-to-right direction
我有一系列英语和阿拉伯语文本,应该以对齐的方式打印。
例如:
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]
有没有办法在输出前不显式反转列表而从左到右打印阿拉伯字符?
您需要在每个 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) + "]";
}
我有一系列英语和阿拉伯语文本,应该以对齐的方式打印。
例如:
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]
有没有办法在输出前不显式反转列表而从左到右打印阿拉伯字符?
您需要在每个 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) + "]";
}