7

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

2 Answers2

12

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.

Takarii
  • 1,612
  • 2
  • 18
  • 29
  • The caveat here is that the direction is forced to an RTL context by this string. If you're writing in an RTL context that's fine, but you embed this string in a LTR context then you'll be flipping its direction. Using the embedded codes allows you locally override the formatting but then remove the override when you are done, leaving the surrounding context unchanged, regardless of whether it was LTR or RTL to begin with. – J... Aug 25 '16 at 14:03
  • simple answer , var result = 10 + "/" + abbreviation + lefttoright + "/" + 2016; thank you – Mohammad Qurashi Aug 25 '16 at 14:31
  • ok but the %u200E is appears on the url is there any solution for not make it appear – Marwan Dec 15 '16 at 13:53
  • @Marwan You should ask that as a separate question, i think your requirement might be different. Reference this answer in your question so people can see what you mean – Takarii Dec 16 '16 at 08:49
  • Something is missing here. In your last line of code you're using the variable 'abbreviation' which was not declared and you're missing the variable 'followUpFormula'. – Dov Miller Mar 19 '18 at 14:10
4

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.

J...
  • 30,968
  • 6
  • 66
  • 143