How to print Arabic characters in left-to-right direction

£可爱£侵袭症+ 提交于 2019-12-04 00:48:21

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!