英文:
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 = "10-م-20";
let parts = s.split("-");
let new_parts = [parts[0], "م", parts[2]];
let new_s = new_parts.join("-");
console.log(new_s); // Output was: "10-م-20"
<!-- 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 = "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);
<!-- end snippet -->
Outputs
> // 10-م-20
Note: you can replace the hardcoded م with parts[1]
if desired:
let new_parts = ["\u202A" + parts[0], "\u202A" + parts[1], "\u202A" + 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("10-\u202Aم-\u202A20")
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论