2015-04-16 3 views
10

Ho una sequenza di testo inglese e arabo che deve essere stampata in modo allineato.Come stampare caratteri arabi nella direzione sinistra-destra

Ad esempio:

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); 

Output previsto:

ArArray: [ت, ب, ا] // <- I want characters to be printed in the order they were added to the list 
EnArray: [a, b, c] 

effettiva di uscita:

ArArray: [ا, ب, ت] // <- but they're printed in reverse order 
EnArray: [a, b, c] 

Esiste un modo per stampare caratteri arabi a sinistra a -giusta direzione senza invertire esplicitamente la lista prima dell'output?

+1

Se la parte "produzione effettiva" di cui sopra è corretto, sembra che la serie sta invertendo i suoi elementi sulla sua propria. –

+1

@SashaSalauyou controlla di nuovo le uscite effettive e previste. Intendevo stamparlo normalmente da sinistra a destra come lettere inglesi ma in realtà è stato invertito nell'output. – vanilla

+0

@GeorgeT sì è stato annullato ma non voglio che venga invertito perché il testo non è visualizzato come allineato. – vanilla

risposta

10

è necessario aggiungere il left-to-right mark '\u200e' prima di ogni carattere RTL per renderlo stampare 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) + "]"; 
} 
+0

Funzionerebbe senza loop autoprodotto se si scrive System.out.println ("\ u200eArArray:" + ar). Non ho un terminale che supporti l'AR, quindi non posso provarlo. – Torben

+0

@Torben Ho provato ad aggiungerlo appena prima dell'intero output, ma non funziona. Ci sono riuscito solo aggiungendolo prima di ogni personaggio arabo. –

+0

@SashaSalauyou Grande !! funziona come previsto. molte grazie – vanilla