ES6 String对象方法charCodeAt()
ES6 String对象方法charCodeAt()
此方法返回一个数字,指定索引处字符的unicode值。unicode代码点的范围从0到1,114,111。前128个unicode代码点是ascii字符编码的直接匹配。charCodeAt()总是返回小于65,536的值。
语法
string.charCodeAt(index);
参数详解
index − 表示字符串中某个位置的数字,即字符在字符串中的下标。
返回值
返回指定索引处字符的 Unicode 数值(Unicode 编码单元 > 0x10000 的除外)。如果指定的 index 小于 0 或大于字符串的长度,则 charCodeAt 返回 NaN。
例子
var str = new String("This is string"); console.log("str.charAt(0) is:" + str.charCodeAt(0)); console.log("str.charAt(1) is:" + str.charCodeAt(1)); console.log("str.charAt(2) is:" + str.charCodeAt(2)); console.log("str.charAt(3) is:" + str.charCodeAt(3)); console.log("str.charAt(4) is:" + str.charCodeAt(4)); console.log("str.charAt(5) is:" + str.charCodeAt(5));
输出
str.charAt(0) is:84 str.charAt(1) is:104 str.charAt(2) is:105 str.charAt(3) is:115 str.charAt(4) is:32 str.charAt(5) is:105