Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...