10

Screen Shoot of strange java String behavior

So, as you can see from the image, that i have concatenated a,c and b. And i am getting the result i expected. But in 2nd println, when i concatenated a,e and b, i got e concatenated in the end, not where i was expecting it to be. I want to know, the reason of this behavior and solution to this behavior. Thank you in advance.

import java.util.*;
public class prob 
{
    public static void main(String... args)
    {
        String a="الف",b="1/2",c="ب",e="B";

        System.out.println(a+" : "+c+" : "+b);
        System.out.println(a+" : "+e+" : "+b);
    }
}

EDIT(To explain why my question is not a duplicate): My question is on converting L2R languages to R2L.

Zia Ul Rehman Mughal
  • 2,119
  • 24
  • 44

3 Answers3

13

This is because the first character is R2L (right to left orientation as in asian languages), so next character becames at the begining (correct orientation):

First char:

الف 
// actual orientation ←

Second char added at L

// add ←
B : الف 
// actual orientation →

After this, B is L2R as usual in Europe, so next char (1/2) is added in the right orientation AFTER B:

// → add in this direction
B : 1/2 : الف 
// actual orientation → (still)

You can easily test it by copy paste char and writting manually another, you will see how orientation changes depending of the char you inserted.


UPDATE:

what is my solution for this issue, because i made this example only to show what issue i was facing in making some big reports, where data is mix sometimes, it is L2R String and sometimes R2L. And i want to make a string in strictly this format.(

From this answer:

  • Left-to-right embedding (U+202A)
  • Right-to-left embedding (U+202B)
  • Pop directional formatting (U+202C)

So in java, to embed a RTL language like Arabic in an LTR language like English, you would do

myEnglishString + "\u202B" + myArabicString + "\u202C" + moreEnglish

and to do the reverse

myArabicString + "\u202A" + myEnglishString + "\u202C" + moreArabic

See (for the source material)


ADD ON 2:

char l2R = '\u202A';
System.out.println(l2R + a + " : " + e +" : "+b);

OUTPUT:

‪الف : B : 1/2
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
3

The reason, as it's said already in this answer, is the fact that some string have a right-to-left orientation.

You can manually set the orientation to letf-to-right for the string with right-to-left orientation, with \u200e control character, like:

String a="\u200eالف",b="1/2",c="\u200eب",e="B";

System.out.println(a+" : "+c+" : "+b);
System.out.println(a+" : "+e+" : "+b);
Community
  • 1
  • 1
Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • I tried it but i am not getting desired output with System.out.println(a+" : "+("\u200e"+e)+" : "+b); and System.out.println(a+" : "+"\u200e"+e+" : "+b); Why i am settign it in the function has a reason, that this is just an example program to explain the behaviour, which i was facing in an application where i need to make and store(for later printing usage) Strings which can contain either L2R or R2L strings, and i need to concat them in correct order – Zia Ul Rehman Mughal Oct 19 '15 at 15:41
  • this wont work because is not a concatenation of strings, so orientation changes wont affect – Jordi Castilla Oct 19 '15 at 15:43
  • So, i need to use concat funtion? – Zia Ul Rehman Mughal Oct 19 '15 at 15:48
  • @ZiaUlRehmanMughal you have to modify not the e vatiable, but other variables like System.out.println("\u200e"+a+" : "+e+" : "+"\u200e"+b); e is already has a l2r orientation or a whole string System.out.println("\u200e"+a+" : "+e+" : "+b); – Stanislav Oct 19 '15 at 15:48
  • This reverses whole of my string concatination order. – Zia Ul Rehman Mughal Oct 19 '15 at 15:49
  • @ZiaUlRehmanMughal you just have to provide the order you neede with adding `\u200e` l2r and `\u202e` r2l formatting as you need it. – Stanislav Oct 19 '15 at 15:53
  • 1
    @JordiCastilla this changes the r2l orientation of all r2l-orientated strings to l2r, that means, all the strings now are l2r-orientated and that won't cause any problems with orientation. why do you suppose, you have to use concatenation? – Stanislav Oct 19 '15 at 15:57
  • To differenciate special char from string content – Jordi Castilla Oct 19 '15 at 16:01
  • 1
    @JordiCastilla agree, that it could look more obviously. But to use concatenation in case, you create a new fixed string is a little bit expensive sometimes. – Stanislav Oct 19 '15 at 16:09
0

I have tried all of the other answers but nothing worked for me. Instead of the concatenation of strings, I used the StringBuilder class.

StringBuilder append = new StringBuilder(" Leftover").append('\u202A').append("الضريبة").append('\u202C').append("Thermalization");
Eric Aya
  • 69,473
  • 35
  • 181
  • 253