本文介绍了如何在左右方向上打印阿拉伯字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a sequence of English and Arabic text that should be printed in an aligned way.

例如:

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]

有没有一种方法可以从左到右打印阿拉伯字符,而无需在输出之前明确地反转列表?

Is there a way to print Arabic characters in left-to-right direction without explicitly reversing the list before output?

推荐答案

您需要添加在每个RTL字符之前将其从左到右标记'\ u200e' 使其打印成LTR:

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) + "]";
}

这篇关于如何在左右方向上打印阿拉伯字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:45