js merge text in hebrew

48 views Asked by At

i am trying to merge two string in hebrew with letter and number , the string Does not connect to a correct string and the number and sign - moves to the end of the sentence

the result should have been "בנק ישראל ב - 8752154 בדיקת טסט נוסף"

and the result is : 8752154 - בנק ישראל ב בדיקת טסט נוסף

var result="";
var field = "8752154 - בנק ישראל ב";
var text1=" בדיקת טסט נוסף ";
result = field + text1 ;
console.log(result);
2

There are 2 answers

0
Abion47 On

The strings will be concatenated in the order in which you specify. For example:

let a = 'abc'; 
let b = 'def';

let c = a + b;
let d = b + a;

console.log(c); // "abcdef"
console.log(d); // "defabc";

If you want to reverse the order in which the strings are concatenated, then reverse the order in which you concatenate them.

let field = "8752154 - בנק ישראל ב";
let text1 = " בדיקת טסט נוסף ";

let result1 = field + text1;
let result2 = text1 + field;

console.log(result1); // "8752154 - בנק ישראל ב בדיקת טסט נוסף "
console.log(result2); // " בדיקת טסט נוסף 8752154 - בנק ישראל ב"
0
Eloi On

I'm not sure why this happens but if you don't mind the cost of some extra steps you can split each string in order to guarantee that every 'word' is in place

this is done like this:

var result="";
var field = "8752154 - בנק ישראל ב";
var text1=" בדיקת טסט נוסף ";

var fieldWords = field.split(' '); // ["8752154", "-", "בנק", "ישראל", "ב"]
var textWords = text1.split(' '); //["", "בדיקת", "טסט", "נוסף", ""]
var Words = [...textWords, ...fieldWords];

result = Words.join(' ');
console.log(result); // output:  בדיקת טסט נוסף  8752154 - בנק ישראל ב