静态
String.fromCharCode()
method returns a string created from the specified sequence of UTF-16 code units.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
String.fromCharCode(num1[, ...[, numN]])
num1, ..., num
N
0
and
65535
(
0xFFFF
). Numbers greater than
0xFFFF
are truncated. No validity checks are performed.
A string of length
N
consisting of the
N
specified UTF-16 code units.
This method returns a string and not a
String
对象。
因为
fromCharCode()
is a static method of
String
, you always use it as
String.fromCharCode()
, rather than as a method of a
String
object you created.
In UTF-16, the most common characters can be represented by a single 16-bit value (i.e. a code unit). However, this set of characters, known as the Base Multilingual Plane (BMP), is only
1
/
17
th
of the total addressable Unicode code points. The remaining code points, in the range of
65536
(
0x010000
) to
1114111
(
0x10FFFF
) are known as supplementary characters. In UTF-16, supplementary characters are represented by two 16-bit code units, known as surrogates, that were reserved for this purpose. A valid combination of two surrogates used to represent a supplementary character is known as a surrogate pair.
因为
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()
(part of the ES2015 standard), 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".
fromCharCode()
BMP characters, in UTF-16, use a single code unit:
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
Complete UTF 16 Table
.
Supplementary characters, in UTF-16, require two code units (i.e. a surrogate pair):
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"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'String.fromCharCode' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fromCharCode
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
String.fromCodePoint()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()