javascript - codePointAt 和 charCodeAt 的区别

标签 javascript string codepoint

String.prototype.codePointAt() 和有什么区别和 String.prototype.charCodeAt()在 JavaScript 中?

'A'.codePointAt(); // 65
'A'.charCodeAt();  // 65

最佳答案

来自 charCodeAt 上的 MDN 页面:

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

The UTF-16 code unit matches the Unicode code point for code points which can be represented in a single UTF-16 code unit. If the Unicode code point cannot be represented in a single UTF-16 code unit (because its value is greater than 0xFFFF) then the code unit returned will be the first part of a surrogate pair for the code point. If you want the entire code point value, use codePointAt().


TLDR;
  • charCodeAt() UTF-16
  • codePointAt() 统一码 .
  • 关于javascript - codePointAt 和 charCodeAt 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36527642/

    相关文章:

    javascript - 单击浏览器后退按钮或移动设备后退按钮时保留所选选项卡

    javascript - ajax 成功回调不按预期顺序执行

    c++ - 如何调整属于某个类的唯一指针的 char 数组的大小。它必须在程序的整个生命周期中保持事件状态

    java - 如何阅读非 BMP (astral) Unicode 增补字符(代码点)

    java - 如何将 unicode hex "0x20000"的字符串表示形式转换为 Java 中的 int 代码点 0x20000

    javascript - 如何将图像、名称和文件大小作为 href 附加到表 td

    javascript - JavaScript 中的套索工具

    python - 如何使用Python将unicode字符串中的重音字符转换为其unicode字符代码?

    C编程错误: expected '{' before string constant

    c# - 如何将字符串转换为枚举