Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. JavaScript
  3. JavaScript 参考
  4. JavaScript 标准内置对象
  5. String
  6. String.prototype.charCodeAt()

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

String.prototype.charCodeAt()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

StringcharCodeAt() 方法返回一个整数,表示给定索引处的 UTF-16 码元,其值介于065535 之间。

charCodeAt() 方法总是将字符串当作UTF-16 码元序列进行索引,因此它可能返回单独代理项(lone surrogate)。如果要获取给定索引处的完整 Unicode 码位,请使用String.prototype.codePointAt() 方法。

尝试一下

const sentence = "The quick brown fox jumps over the lazy dog.";const index = 4;console.log(  `Character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(    index,  )}`,);// Expected output: "Character code 113 is equal to q"

语法

js
charCodeAt(index)

参数

index

要返回的字符的索引,从零开始。将被转换为整数——undefined 被转换为 0。

返回值

一个整数,介于065535 之间,表示指定index 处字符的 UTF-16 码元值。如果index 超出了0str.length - 1 的范围,则charCodeAt() 返回NaN

描述

字符串中的字符从左到右进行索引。第一个字符的索引为0,而在名为str 的字符串中,最后一个字符的索引为str.length - 1

Unicode 码位的范围是011141110x10FFFF)。charCodeAt() 方法始终返回一个小于65536 的值,因为更高的码位由一对 16 位代理伪字符(surrogate pseudo-character)来表示。因此,为了获取值大于65535 的完整字符,不仅需要检索charCodeAt(i),而且还要使用charCodeAt(i + 1)(就像操作具有两个字符的字符串一样),或者使用codePointAt(i) 方法。有关 Unicode 的信息,请参见UTF-16 字符、Unicode 码位和字素簇

示例

使用 charCodeAt()

以下示例返回65,即 A 的 Unicode 值。

js
"ABC".charCodeAt(0); // 返回 65

charCodeAt() 可能会返回单独代理项,它们不是有效的 Unicode 字符。

js
const str = "𠮷𠮾";console.log(str.charCodeAt(0)); // 55362 或 d842,不是有效的 Unicode 字符console.log(str.charCodeAt(1)); // 57271 或 dfb7,不是有效的 Unicode 字符

要获取给定索引处的完整 Unicode 码位,请使用String.prototype.codePointAt() 方法。

js
const str = "𠮷𠮾";console.log(str.codePointAt(0)); // 134071

备注:避免使用charCodeAt() 来重新实现codePointAt()。从 UTF-16 代理到 Unicode 码位的转换相当复杂,而且codePointAt() 可能更加高效,因为它直接使用字符串的内部表示形式。如果需要,可以安装一个codePointAt() 的 polyfill。

以下是将一对 UTF-16 码元转换为 Unicode 码位的可能算法,改编自Unicode 常问问题

js
// 常量const LEAD_OFFSET = 0xd800 - (0x10000 >> 10);const SURROGATE_OFFSET = 0x10000 - (0xd800 << 10) - 0xdc00;function utf16ToUnicode(lead, trail) {  return (lead << 10) + trail + SURROGATE_OFFSET;}function unicodeToUTF16(codePoint) {  const lead = LEAD_OFFSET + (codePoint >> 10);  const trail = 0xdc00 + (codePoint & 0x3ff);  return [lead, trail];}const str = "𠮷";console.log(utf16ToUnicode(str.charCodeAt(0), str.charCodeAt(1))); // 134071console.log(str.codePointAt(0)); // 134071

规范

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.charcodeat

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp