String.prototype.charAt()

String 值的 charAt() 方法返回一个新字符串,该字符串由给定索引处的单个 UTF-16 代码单元组成。

¥The charAt() method of String values returns a new string consisting of the single UTF-16 code unit at the given index.

charAt() 始终将字符串索引为 UTF-16 代码单元 的序列,因此它可能返回单独的代理项。要获取给定索引处的完整 Unicode 代码点,请使用 String.prototype.codePointAt()String.fromCodePoint()

¥charAt() always indexes the string as a sequence of UTF-16 code units, so it may return lone surrogates. To get the full Unicode code point at the given index, use String.prototype.codePointAt() and String.fromCodePoint().

Try it

语法

¥Syntax

js
charAt(index)

参数

¥Parameters

index

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

返回值

¥Return value

表示指定 index 处的字符(恰好是一个 UTF-16 代码单元)的字符串。如果 index 超出 0str.length - 1 范围,则 charAt() 返回空字符串。

¥A string representing the character (exactly one UTF-16 code unit) at the specified index. If index is out of the range of 0str.length - 1, charAt() returns an empty string.

描述

¥Description

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

¥Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string called str is str.length - 1.

Unicode 代码点范围从 01114111 (0x10FFFF)。charAt() 始终返回值小于 65536 的字符,因为较高的代码点由一对 16 位代理伪字符表示。因此,为了获得值大于 65535 的完整字符,不仅需要检索 charAt(i),还需要检索 charAt(i + 1)(就像操作两个字符的字符串一样),或者使用 codePointAt(i)String.fromCodePoint() 代替。有关 Unicode 的信息,请参阅 UTF-16 字符、Unicode 代码点和字素簇

¥Unicode code points range from 0 to 1114111 (0x10FFFF). charAt() always returns a character whose value is less than 65536, because the higher code points are represented by a pair of 16-bit surrogate pseudo-characters. Therefore, in order to get a full character with value greater than 65535, it is necessary to retrieve not only charAt(i), but also charAt(i + 1) (as if manipulating a string with two characters), or to use codePointAt(i) and String.fromCodePoint() instead. For information on Unicode, see UTF-16 characters, Unicode code points, and grapheme clusters.

charAt() 与使用 括号表示法 访问指定索引处的字符非常相似。主要区别是:

¥charAt() is very similar to using bracket notation to access a character at the specified index. The main differences are:

  • charAt() 尝试将 index 转换为整数,而括号表示法则不会,而是直接使用 index 作为属性名称。
  • 如果 index 超出范围,charAt() 返回空字符串,而括号表示法返回 undefined

示例

¥Examples

使用 charAt()

¥Using charAt()

以下示例显示字符串 "Brave new world" 中不同位置的字符:

¥The following example displays characters at different locations in the string "Brave new world":

js
const anyString = "Brave new world";
console.log(`The character at index 0   is '${anyString.charAt()}'`);
// No index was provided, used 0 as default

console.log(`The character at index 0   is '${anyString.charAt(0)}'`);
console.log(`The character at index 1   is '${anyString.charAt(1)}'`);
console.log(`The character at index 2   is '${anyString.charAt(2)}'`);
console.log(`The character at index 3   is '${anyString.charAt(3)}'`);
console.log(`The character at index 4   is '${anyString.charAt(4)}'`);
console.log(`The character at index 999 is '${anyString.charAt(999)}'`);

这些行显示以下内容:

¥These lines display the following:

The character at index 0   is 'B'

The character at index 0   is 'B'
The character at index 1   is 'r'
The character at index 2   is 'a'
The character at index 3   is 'v'
The character at index 4   is 'e'
The character at index 999 is ''

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

¥charAt() may return lone surrogates, which are not valid Unicode characters.

js
const str = "𠮷𠮾";
console.log(str.charAt(0)); // "\ud842", which is not a valid Unicode character
console.log(str.charAt(1)); // "\udfb7", which is not a valid Unicode character

要获取给定索引处的完整 Unicode 代码点,请使用按 Unicode 代码点(例如 String.prototype.codePointAt()传播字符串)拆分为 Unicode 代码点数组的索引方法。

¥To get the full Unicode code point at the given index, use an indexing method that splits by Unicode code points, such as String.prototype.codePointAt() and spreading strings into an array of Unicode code points.

js
const str = "𠮷𠮾";
console.log(String.fromCodePoint(str.codePointAt(0))); // "𠮷"
console.log([...str][0]); // "𠮷"

注意:避免使用 charAt() 重新实现上述解决方案。单独代理及其配对的检测很复杂,内置 API 的性能可能更高,因为它们直接使用字符串的内部表示形式。如有必要,为上述 API 安装一个 polyfill。

¥Note: Avoid re-implementing the solutions above using charAt(). The detection of lone surrogates and their pairing is complex, and built-in APIs may be more performant as they directly use the internal representation of the string. Install a polyfill for the APIs mentioned above if necessary.

规范

Specification
ECMAScript Language Specification
# sec-string.prototype.charat

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看