gpt4 book ai didi

javascript - 有没有办法从单个字符中获取 charCode?

转载 作者:行者123 更新时间:2023-12-05 00:37:49 26 4
gpt4 key购买 nike

我喜欢这个功能String.prototype.charCodeAt , 但因为它是 String 上的一种方法你必须提供一个索引参数。我知道 Ecmascript 不处理单个字符,但是有没有 “原生”直接从一个字符获取charCode的方法?像 str[0].charCode()或任何考虑 1 长度字符串的东西?

最佳答案

您应该首先定义在您的情况下 charCode 是什么意思。
JS 使用 UTF-16 编码的字符串,所以 charCodeAt是该位置的 UTF-16 代码单元的值。最初 JS 使用 UCS2 子集,其中 charCodeAt()有完美的感觉。不再完全支持 UCS4 编码为 UTF-16。

从字符串中获取真正的 UNICODE 代码点(是你的 charCode 吗?)的唯一方法是使用 ES6 及其字符串枚举功能:

for (let ch of str) { ch here is real UNICODE code point }

注意,上面的迭代可能比 str.length 少。

并查看 String.prototype.codePointAt() .

关于javascript - 有没有办法从单个字符中获取 charCode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35561324/

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