I have a string that contains an Arabic character, I want the character to be in the middle of the string but it keeps putting it at the end

huangapple go评论68阅读模式
英文:

I have a string that contains an Arabic character, I want the character to be in the middle of the string but it keeps putting it at the end

问题

我需要在我的前端页面中放置一个包含两个数字和一个阿拉伯字符的字符串,这两个数字应该将阿拉伯字符放在中间。我尝试了一些不同的方法来实现这个目标,但它们都将阿拉伯字符放在字符串的末尾。

我尝试了以下作为解决方案,但仍然不起作用,你们可以帮助我吗?

let s = "10-م-20";
let parts = s.split("-");
let new_parts = [parts[0], "م", parts[2]];
let new_s = new_parts.join("-");
console.log(new_s);  // 输出为:"10-م-20"

请告诉我,这是否有助于您解决问题。

英文:

I need to put a string in my frontend that contains two numbers and an Arabic character, these numbers should have the Arabic character in the middle, i tried some different methods to do this, but they all put the Arabic character at the end of the string;

I tried this as a solution but it is still not working, can you guys help me out with this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let s = &quot;10-م-20&quot;;
let parts = s.split(&quot;-&quot;);
let new_parts = [parts[0], &quot;م&quot;, parts[2]];
let new_s = new_parts.join(&quot;-&quot;);
console.log(new_s);  // Output was: &quot;10-م-20&quot; 

<!-- end snippet -->

答案1

得分: 4

您可以使用POP DIRECTIONAL FORMATTING,在字符串前面加上\u202A,如下所示:

let s = "10-م-20";
let parts = s.split("-");
let new_parts = ["\u202A" + parts[0], "\u202A" + "م", "\u202A" + parts[2]];
let new_s = new_parts.join("-");
console.log(new_s);

输出

// 10-‪م-‪20

注意:如果需要,您可以使用parts[1]替换硬编码的م:

let new_parts = ["\u202A" + parts[0], "\u202A" + parts[1], "\u202A" + parts[2]];

如果只需创建字符串10-‪م-‪20,则可以简化为以下内容,无需拆分或连接:

console.log("10-\u202Aم-\u202A20")
英文:

You can use POP DIRECTIONAL FORMATTING, which is where you prepend the string with \u202A like so:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let s = &quot;10-م-20&quot;;
let parts = s.split(&quot;-&quot;);
let new_parts = [&quot;\u202A&quot; + parts[0], &quot;\u202A&quot; + &quot;م&quot;, &quot;\u202A&quot; + parts[2]];
let new_s = new_parts.join(&quot;-&quot;);
console.log(new_s);

<!-- end snippet -->

Outputs
> // 10-‪م-‪20

Note: you can replace the hardcoded م with parts[1] if desired:

let new_parts = [&quot;\u202A&quot; + parts[0], &quot;\u202A&quot; + parts[1], &quot;\u202A&quot; + parts[2]];

If you simply need to create the string 10-‪م-‪20 then you can simplify it to this, without any splitting or joining:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(&quot;10-\u202Aم-\u202A20&quot;)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 19:43:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定