字符串codePointAt()方法

This was introduced in ES2015 to handle Unicode characters that cannot be represented by a single 16-bit Unicode unit, but need 2 instead.

ES2015中引入了此功能,以处理无法由单个16位Unicode单元表示但需要2个字符的Unicode字符。

Using charCodeAt() you need to retrieve the first, and the second, and combine them. Using codePointAt() you get the whole character in one call.

使用charCodeAt()您需要检索第一个和第二个,并将它们合并。 使用codePointAt()可以一次调用整个字符。

For example, this chinese character “𠮷” is composed by 2 UTF-16 (Unicode) parts:

例如,此汉字“𠮷”由2个UTF-16(Unicode)组成:

"𠮷".charCodeAt(0).toString(16) //d842
"𠮷".charCodeAt(1).toString(16) //dfb7

If you create a new character by combining those unicode characters:

如果通过组合这些unicode字符来创建新字符:

"\ud842\udfb7" //"𠮷"

You can get the same result usign codePointAt():

您可以使用codePointAt()获得相同的结果:

"𠮷".codePointAt(0) //20bb7

If you create a new character by combining those unicode characters:

如果通过组合这些unicode字符来创建新字符:

"\u{20bb7}" //"𠮷"

More on Unicode and working with it in Unicode and UTF-8.

有关Unicode以及在Unicode和UTF-8中使用它的更多信息。

翻译自: https://flaviocopes.com/javascript-string-codepointat/

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值