pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt

cript src="/static/client/4585.458a2b03d77c78d5.js" type="module">

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

View in English Always switch to English

String.prototype.charCodeAt()

基线 广泛可用

自 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))); // 134071
console.log(str.codePointAt(0)); // 134071

规范

规范
ECMAScript® 2027 Language Specification
# sec-string.prototype.charcodeat

浏览器兼容性

参见

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy