gpt4 book ai didi

javascript - 如何从 JavaScript 中的表情符号中获取真正的字符代码?

转载 作者:行者123 更新时间:2023-12-01 15:55:27 25 4
gpt4 key购买 nike

例如,我有这个表情符号:

📝

...我想获取您的字符代码。我试过这个:

var emoji = "📝"
var char_code = emoji.charCodeAt()
console.log(emoji,char_code)

但是当使用 String 的 fromCharCode 方法时,我没有得到原始的表情符号:

var emoji = "📝"
var char_code = emoji.charCodeAt()
var original = String.fromCharCode(char_code)
console.log(original)

如何从您的字符代码中获取原始表情符号?或者我怎样才能获得您的实际字符代码以在 String.fromCharCode 中使用它?

最佳答案

String.codePointAt()String.fromCodePoint() 就是为此而设计的,尽管也可以指定 UTF-16 charCode 在旧版浏览器中使用 surrogate keypairs (参见讨论 here)。

let emoji = "📝",
charCode = emoji.charCodeAt(),
codePoint = emoji.codePointAt();

console.log(
'charCode:', charCode,
String.fromCharCode(charCode) // doesn't work =(
)

console.log(
'codePoint:', codePoint,
String.fromCodePoint(codePoint) // there we go!
);

关于javascript - 如何从 JavaScript 中的表情符号中获取真正的字符代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62858054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com