charCodeAt()
method returns an integer between
0
and
65535
representing the UTF-16 code unit at the given index.
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.
The UTF-16 code unit matches the Unicode code point for code points which can be represented in a single UTF-16 code unit. If the Unicode code point cannot be represented in a single UTF-16 code unit (because its value is greater than
0xFFFF
) then the code unit returned will be
the first part of a surrogate pair
for the code point. If you want the entire code point value, use
codePointAt()
.
str.charCodeAt(index)
index
0
and less than the
length
of the string. If
index
is not a number, it defaults to
0
.
A number representing the UTF-16 code unit value of the character at the given
index
。若
index
is out of range,
charCodeAt()
返回
NaN
.
Unicode code points range from
0
to
1114111
(0x10FFFF
). The first 128 Unicode code points are a direct match of the ASCII character encoding. (For information on Unicode, see the
JavaScript 指南
.)
注意:
charCodeAt()
will always return a value that is less than
65536
. This is because the higher code points are represented by
a pair
of (lower valued) "surrogate" pseudo-characters which are used to comprise the real character.
Because of this, in order to examine (or reproduce) the full character for individual character values of
65536
or greater, for such characters, it is necessary to retrieve not only
charCodeAt(
i
)
, but also
charCodeAt(
i
+1)
(as if manipulating a string with two letters), or to use
codePointAt(
i
)
instead. See examples 2 and 3 (below).
charCodeAt()
返回
NaN
if the given index is less than
0
, or if it is equal to or greater than the
length
of the string.
Backward compatibility: In historic versions (like JavaScript 1.2) the
charCodeAt()
method returns a number indicating the ISO-Latin-1 codeset value of the character at the given index. The ISO-Latin-1 codeset ranges from
0
to
255
. The first
0
to
127
are a direct match of the ASCII character set.
charCodeAt()
The following example returns
65
, the Unicode value for A.
'ABC'.charCodeAt(0) // returns 65
charCodeAt()
to handle non-Basic-Multilingual-Plane characters if their presence earlier in the string is unknown
This version might be used in for loops and the like when it is unknown whether non-BMP characters exist before the specified index position.
function fixedCharCodeAt(str, idx) {
// ex. fixedCharCodeAt('\uD800\uDC00', 0); // 65536
// ex. fixedCharCodeAt('\uD800\uDC00', 1); // false
idx = idx || 0;
var code = str.charCodeAt(idx);
var hi, low;
// High surrogate (could change last hex to 0xDB7F
// to treat high private surrogates
// as single characters)
if (0xD800 <= code && code <= 0xDBFF) {
hi = code;
low = str.charCodeAt(idx + 1);
if (isNaN(low)) {
throw 'High surrogate not followed by ' +
'low surrogate in fixedCharCodeAt()';
}
return (
(hi - 0xD800) * 0x400) +
(low - 0xDC00) + 0x10000;
}
if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
// We return false to allow loops to skip
// this iteration since should have already handled
// high surrogate above in the previous iteration
return false;
// hi = str.charCodeAt(idx - 1);
// low = code;
// return ((hi - 0xD800) * 0x400) +
// (low - 0xDC00) + 0x10000;
}
return code;
}
charCodeAt()
to handle non-Basic-Multilingual-Plane characters if their presence earlier in the string is known
function knownCharCodeAt(str, idx) {
str += '';
var code,
end = str.length;
var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
while ((surrogatePairs.exec(str)) != null) {
var li = surrogatePairs.lastIndex;
if (li - 2 < idx) {
idx++;
}
else {
break;
}
}
if (idx >= end || idx < 0) {
return NaN;
}
code = str.charCodeAt(idx);
var hi, low;
if (0xD800 <= code && code <= 0xDBFF) {
hi = code;
low = str.charCodeAt(idx + 1);
// Go one further, since one of the "characters"
// is part of a surrogate pair
return ((hi - 0xD800) * 0x400) +
(low - 0xDC00) + 0x10000;
}
return code;
}
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'String.prototype.charCodeAt' 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 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
charCodeAt
|
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.fromCharCode()
String.prototype.charAt()
String.fromCodePoint()
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()