2

I'm facing trouble in concatenating Arabic string with English string but their order is being messed!

I tried + operator and str1.concat(..) but nothing works for me.

var a = 'english'
var b = 'أ.ب-000082-13'
var c = '000004-ر خ-2014.xml'

//var myCoolString =a + '\\' + b + '\\' + c;

var myCoolString =a.concat("\\",b,"\\",c) 

document.getElementsByTagName('output')[0].innerHTML = myCoolString;

The result was like this: english\أ.ب-000082-13\000004-ر خ-2014.xml

user2638062
  • 73
  • 2
  • 8
  • May be helpful: http://stackoverflow.com/questions/6177294/string-concatenation-containing-arabic-and-western-characters .. however, are your numbers written LTR? you may find you have the direction changing within each string too – Paul S. May 01 '15 at 14:07

1 Answers1

14

The characters your are looking for are \u202A, \u202B and \u202C

function wrap_dir(dir, str) {
    if (dir === 'rtl') return '\u202B' + str + '\u202C';
    return '\u202A' + str + '\u202C';
}


wrap_dir('ltr', a) + wrap_dir('ltr', '\\') + wrap_dir('rtl', b) + wrap_dir('ltr', '\\') + wrap_dir('ltr', c);
// "‪english‬‪\‬‫أ.ب-000082-13‬‪\‬‪000004-ر خ-2014.xml‬"

Not sure why c wanted to be LTR, maybe because it ends .xml?

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • Thanks Paul! I know this is an old thread and this answer works great but something I encountered that I would like to share with everyone. If anyone is planning to use the wrapped text in the browser URL make sure you escape the characters added in this function so it appears correctly. Thank you – mnaa Jan 23 '21 at 20:34