2

I have arabic string with some special signs:

const str = "- زيادة زمن التصويب تحت المنظار (+۱۰%)";

const arr = [];
for(let i=0;i<str.length;i++){
    arr.push(str[i]);
}
console.log(arr);

But the result of that is a messed up array of: ['-', ' ', 'ز', 'ي', 'ا', 'د', 'ة', ' ', 'ز', 'م', 'ن', ' ', 'ا', 'ل', 'ت', 'ص', 'و', 'ي', 'ب', ' ', 'ت', 'ح', 'ت', ' ', 'ا', 'ل', 'م', 'ن', 'ظ', 'ا', 'ر', ' ', '(', '+', '۱', '۰', '%', ')']

Any idea how to maintain the order of each sign?

Kruton
  • 43
  • 4
  • If you want to split the string into an array of characters, use `,.split('')`. What you're getting in that array _is_ the characters' current order... – Cerbrus Jun 22 '22 at 14:12
  • See https://stackoverflow.com/questions/58074813/how-does-javascript-split-work-on-arabic-plus-english-number-strings – Ruan Mendes Jun 22 '22 at 14:15
  • split also doesn't work. As you can see I even accessed them by indexes and its wrong order – Kruton Jun 22 '22 at 14:21
  • There's a difference between the character order in _data_, and in how it's _rendered_, when Arabic is concerned. What you're seeing is the _data_ order. – Cerbrus Jun 22 '22 at 14:25
  • @Cerbrus what if I want to display each letter in separate div? – Kruton Jun 22 '22 at 14:28
  • I don't know how that'd render. Maybe spans will behave themselves as you'd want to. – Cerbrus Jun 22 '22 at 14:29
  • Yea, spans would render them as if it's one string: https://jsbin.com/govodetihe/edit?html,css,js,output – Cerbrus Jun 22 '22 at 14:34
  • @Cerbrus that would be a great solution! Unfortunately the engine I'm working on doesn't support spans. Inline elements in general – Kruton Jun 22 '22 at 15:01
  • @Kruton this seems to be a console text wrap problem plus incorrect RTL text storage. The array content is correct. – Mohsen Alyafei Jul 18 '22 at 07:43

1 Answers1

0

When running this in the StackOverflow console, the display is correct and the array stores the characters correctly. Please see the below example.

It seems that your console wraps the output when displaying the content of the array. I think you then tried to join the lines together to produce one line output for the purpose of the question.

I think you had a console output like this in your console (example in VSCode console):

enter image description here

Try running your code again here:

const str = "- زيادة زمن التصويب تحت المنظار (+۱۰%)";

const arr = [];
for(let i=0;i<str.length;i++){
    arr.push(str[i]);
}
console.log(arr);

Please note that this Arabic text you provided is not stored as RTL but seems to be LTR as the last character "hyphen" should appear at the right of the text if it was in RTL format.

Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42