我想使用索引从 unicode 字符串中获取特定字母。但是,它并没有按预期工作。
Example:
var handwriting = `????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1234567890`
var normal = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`
console.log(normal[3]) // gives 'd' but
console.log(handwriting[3]) // gives '�' instead of '????'
长度也没有按预期工作normal.length
给出正确的值62 but handwriting.length
gives 114.
索引未按预期工作。如何访问 unicode 数组的元素?
我在 python 上尝试过,它工作得很好,但在 Javascript 中却不起作用。
我需要 unicode 字符串中的确切字符,例如索引 3 的 'd' '????' 的预期输出
在 Javascript 中,字符串是 16 位代码点的序列。由于这些字符是在基本多语言平面之上编码的,这意味着它们由一对代码点(也称为代理对)表示。
参考 https://thekevinscott.com/emojis-in-javascript/
统一码编号????
is U+1D586
。并且 0x1D586 大于 0xFFFF (2^16)。所以,????
由一对代码点表示,也称为代理对
console.log("????".length)
console.log("????" === "\uD835\uDD86")
一种方法是使用扩展语法创建字符数组或Array.from()
然后得到你需要的索引
var handwriting = `????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1234567890`
console.log([...handwriting][3])
console.log(Array.from(handwriting)[3])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)