String.fromCharCode()

String.fromCharCode() 静态方法返回根据指定的 UTF-16 代码单元序列创建的字符串。

¥The String.fromCharCode() static method returns a string created from the specified sequence of UTF-16 code units.

Try it

语法

¥Syntax

js
String.fromCharCode()
String.fromCharCode(num1)
String.fromCharCode(num1, num2)
String.fromCharCode(num1, num2, /* …, */ numN)

参数

¥Parameters

num1, …, numN

065535 (0xFFFF) 之间的数字,表示 UTF-16 代码单元。大于 0xFFFF 的数字将被截断到最后 16 位。不执行有效性检查。

返回值

¥Return value

长度为 N 的字符串,由 N 指定的 UTF-16 代码单元组成。

¥A string of length N consisting of the N specified UTF-16 code units.

描述

¥Description

由于 fromCharCode()String 的静态方法,因此你始终将其用作 String.fromCharCode(),而不是用作你创建的 String 值的方法。

¥Because fromCharCode() is a static method of String, you always use it as String.fromCharCode(), rather than as a method of a String value you created.

Unicode 代码点范围从 01114111 (0x10FFFF)。charCodeAt() 始终返回小于 65536 的值,因为较高的代码点由一对 16 位代理伪字符表示。因此,为了产生值大于 65535 的完整字符,需要提供两个代码单元(就像操作两个字符的字符串一样)。有关 Unicode 的信息,请参阅 UTF-16 字符、Unicode 代码点和字素簇

¥Unicode code points range from 0 to 1114111 (0x10FFFF). charCodeAt() always returns a value that is less than 65536, because the higher code points are represented by a pair of 16-bit surrogate pseudo-characters. Therefore, in order to produce a full character with value greater than 65535, it is necessary to provide two code units (as if manipulating a string with two characters). For information on Unicode, see UTF-16 characters, Unicode code points, and grapheme clusters.

由于 fromCharCode() 仅适用于 16 位值(与 \u 转义序列相同),因此需要代理对才能返回补充字符。例如,String.fromCharCode(0xd83c, 0xdf03)"\ud83c\udf03" 都返回代码点 U+1F303 "与星星的夜晚"。虽然补充码点值(例如 0x1f303)和代表它的两个代理值(例如 0xd83c0xdf03)之间存在数学关系,但每次都需要额外的步骤来计算或查找代理对值。 将使用补充码点。因此,使用 String.fromCodePoint() 更方便,它允许根据实际代码点值返回补充字符。例如,String.fromCodePoint(0x1f303) 返回代码点 U+1F303 "与星星的夜晚"。

¥Because fromCharCode() only works with 16-bit values (same as the \u escape sequence), a surrogate pair is required in order to return a supplementary character. For example, both String.fromCharCode(0xd83c, 0xdf03) and "\ud83c\udf03" return code point U+1F303 "Night with Stars". While there is a mathematical relationship between the supplementary code point value (e.g. 0x1f303) and both surrogate values that represent it (e.g., 0xd83c and 0xdf03), it does require an extra step to either calculate or look up the surrogate pair values every time a supplementary code point is to be used. For this reason, it's more convenient to use String.fromCodePoint(), which allows for returning supplementary characters based on their actual code point value. For example, String.fromCodePoint(0x1f303) returns code point U+1F303 "Night with Stars".

示例

¥Examples

使用 fromCharCode()

¥Using fromCharCode()

BMP 字符在 UTF-16 中使用单个代码单元:

¥BMP characters, in UTF-16, use a single code unit:

js
String.fromCharCode(65, 66, 67); // returns "ABC"
String.fromCharCode(0x2014); // returns "—"
String.fromCharCode(0x12014); // also returns "—"; the digit 1 is truncated and ignored
String.fromCharCode(8212); // also returns "—"; 8212 is the decimal form of 0x2014

UTF-16 中的增补字符需要两个代码单元(即代理对):

¥Supplementary characters, in UTF-16, require two code units (i.e. a surrogate pair):

js
String.fromCharCode(0xd83c, 0xdf03); // Code Point U+1F303 "Night with
String.fromCharCode(55356, 57091); // Stars" === "\uD83C\uDF03"

String.fromCharCode(0xd834, 0xdf06, 0x61, 0xd834, 0xdf07); // "\uD834\uDF06a\uD834\uDF07"

规范

Specification
ECMAScript Language Specification
# sec-string.fromcharcode

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看