Mohammad Qurashi
Mohammad Qurashi

Reputation: 103

incorrect right to left concatenation english and Arabic

I'm trying to concatenate an English string with Arabic string

string followUpFormula = "FIF";
string renewAbbreviation =  "ع.ت" ;
string abbreviation = followUpFormula +"-"+ renewAbbreviation;
var result = 10 + "/" + abbreviation + "/" + 2016;

the result is 10/FIF-ع.ت/2016 but i want to display them like this: 10/FIF-ع.ت/
2016

how can I do that? thanks

Upvotes: 7

Views: 6606

Answers (2)

Takarii
Takarii

Reputation: 1648

Couple of additions to your code

string followUpFormula = "FIF";
string renewAbbreviation =  "ع.ت" ;
string abbreviation = followUpFormula +"-"+ renewAbbreviation;
var lefttoright = ((Char)0x200E).ToString();
var result = 10 + "/" + abbreviation + lefttoright + "/" + 2016;

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

Char 0x200F switches to a right to left format.

Upvotes: 12

J...
J...

Reputation: 31393

This has to do with the way that unicode process rules about mixing LTR and RTL text. You can override the default behaviour by explicitly using special characters that indicate an intention to directly embed RTL or LTR text :

private const char LTR_EMBED = '\u202A';
private const char POP_DIRECTIONAL = '\u202C';
private string ForceLTR(string inputStr)
{
    return LTR_EMBED + inputStr + POP_DIRECTIONAL;
}

private void Form1_Load(object sender, EventArgs e)
{
    string followUpFormula = "FIF";
    string renewAbbreviation = "ع.ت";
    string abbreviation = ForceLTR(followUpFormula + "-" + renewAbbreviation);
    textBox1.Text = 10 + "/" + abbreviation + "/" + 2016;
}

This places an embedded Left-To-Right character (U+202A) before the string and follows it with a Pop-Directional-Formatting (U+202C) character. The latter removes the embedded directional formatting cue and returns the text direction to whatever it was in the previous context. The returned string, therefore, is safe to use in either an RTL or LTR context.

The rules for parsing LTR and RTL text in various contexts are extensive and complex. For reference you can find the bidirectional algorithm specification here. Certain characters are classified as "weak" or "strong" in terms of their affinity for LTR or RTL contexts. Things like / and - are weak so you have to be explicit when mixing them about which text direction and layout you wish these characters to respect.

Upvotes: 4

Related Questions