7

I am using .Replace() method to replace a placeholder [CITY] in arabic language.

public static void Main()
{
    Console.WriteLine("Hello World");
    var replace = "سنغافورة";
    var input = "ABC [CITY] مرحبا بالعالم";
    Console.WriteLine(input);
    var final = input.Replace("[CITY]", replace);
    Console.WriteLine(final);
}

I get the following output

ABC [CITY] مرحبا بالعالم
ABC سنغافورة مرحبا بالعالم

As you can see the city instead of being placed next to ABC is added at the extreme right.

This issue happens only for arabic and works fine for other languages (english/thai/spanish etc)

Not sure whats going wrong here.

C# fiddle - https://dotnetfiddle.net/mvIcHt

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • 4
    I don't speak or read Arabic, but could this be a problem with Arabic being read right to left? – MindSwipe Jun 13 '19 at 11:32
  • 1
    Does this happen with other languages that use a right-to-left system, e.g. Hebrew? – Lennart Jun 13 '19 at 11:32
  • I bet you'd get the same problem with Hebrew and other right-to-left languages. – Matthew Watson Jun 13 '19 at 11:33
  • Arabic is the only RTL language that this app supports. @Lennart – Yasser Shaikh Jun 13 '19 at 11:33
  • Yes its definitely because of RTL, but how do I fix this :P – Yasser Shaikh Jun 13 '19 at 11:34
  • Does this answer for Hebrew work for Arabic too? https://stackoverflow.com/questions/16313765/how-can-i-get-this-string-replacement-in-hebrew-to-work – Matthew Watson Jun 13 '19 at 11:35
  • `input.Replace(...)` does it work right (if we make dump we'll see that `005b 0043 0049 0054 0059 005d` has been replaced with `0633 0646 063a 0627 0641 0648 0631 0629`). It's rendering that put `مرحبا بالعالم` at the end – Dmitry Bychenko Jun 13 '19 at 11:46
  • Possible duplicate of [incorrect right to left concatenation english and Arabic](https://stackoverflow.com/questions/39146491/incorrect-right-to-left-concatenation-english-and-arabic) – Ian Kemp Jun 13 '19 at 12:42

2 Answers2

2

Using this answer: This

I've edited your code for that:

public static void Main()
{
    Console.WriteLine("Hello World");
    var replace = "سنغافورة";
    var input = "York Hotel في [CITY] – عروض الغرف، صور وتقييمات";
    Console.WriteLine(input);
    var lefttoright = ((Char)0x200E).ToString();
    var final = input.Replace("[CITY]", lefttoright + replace + lefttoright );
    Console.WriteLine(final);

}

And the output is:

Hello World
York Hotel في [CITY] – عروض الغرف، صور وتقييمات
York Hotel في ‎سنغافورة‎ – عروض الغرف، صور وتقييمات

Citing @Takarii:

Char 0x200E is a special character that tells the following text to read left to right see here for more information on the character.

Mikev
  • 2,012
  • 1
  • 15
  • 27
  • Don't copy-paste answers from other questions. Instead, vote to close this question as a duplicate of the one you linked to. – Ian Kemp Jun 13 '19 at 12:42
1

Just add an RTL mark at the beginning of your Arabic text:

public static void Main()
{
    Console.WriteLine("Hello World");

    const char rtl = (char)0x200E;
    var replace = "سنغافورة";

    var input = "York Hotel في [CITY] – " + rtl + "عروض الغرف، صور وتقييمات";
    Console.WriteLine(input);
    var final = input.Replace("[CITY]", replace);
    Console.WriteLine(final);
}

Update: Adopted the answer from here: https://stackoverflow.com/a/44961348/6193089

Dmitri Trofimov
  • 753
  • 3
  • 14
  • Don't copy-paste answers from other questions. Instead, vote to close this question as a duplicate of the one you linked to. – Ian Kemp Jun 13 '19 at 12:42