Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode strings in .Net with Hebrew letters and numbers

There is a strange behavior when trying to create string which contains a Hebrew letter and a digit. The digit will always be displayed left to the letter. For example:

string A = "\u05E9"; //A Hebrew letter
string B = "23";
string AB = A + B;
textBlock1.Text = AB;
//Ouput bug - B is left to A.

This bug only happens when using both a Hebrew letter and digits. When omitting one of those from the equation the bug won't happen:

string A = "\u20AA"; //Some random Unicode.
string B = "23";
string AB = A + B;
textBlock1.Text = AB;
//Output OK.

string A = "\u05E9"; //A Hebrew letter.
string B = "HELLO";
string AB = A + B;
textBlock1.Text = AB;
//Output OK.

I tried playing with FlowDirection property but it didn't help.

A workaround to get the text displayed properly in the first code exmaple would be welcomed.

like image 216
Yaron Levi Avatar asked Jul 06 '11 10:07

Yaron Levi


2 Answers

The unicode characters "RTL mark" (U+200F) and "LTR mark" (U+200E) were created precisely for this purpose.

In your example, simply place an LTR mark after the Hebrew character, and the numbers will then be displayed to the right of the Hebrew character, as you wish.

So your code would be adjusted as follows:

string A = "\u05E9"; //A Hebrew letter
string LTRMark = "\u200E"; 
string B = "23";
string AB = A + LTRMark + B;
like image 71
Avi Shmidman Avatar answered Nov 10 '22 00:11

Avi Shmidman


This is because of Unicode Bidirectional Algorithms. If I understand this correctly, the unicode character has an "identifier" that says where it should be when it's next to another word.

In this case \u05E9 says that it should be to the left. Even if you do:

var ab = string.Format("{0}{1}", a, b);

You will still get it to the left. However, if you take another unicoded character such as \u05D9 this will be added to the right because that character is not said to be on the left.

This is the layout of the language and when outputting this the layout enginge will output it according to the language layout.

like image 40
Filip Ekberg Avatar answered Nov 10 '22 00:11

Filip Ekberg