此方法返回一个数字,该数字指示给定索引处字符的Unicode值。 Unicode代码点的范围是0到1,114,111。前128个Unicode代码点是ASCII字符编码的直接匹配项。 charCodeAt()始终返回小于65,536的值。
charCodeAt() - 语法
string.charCodeAt(index);
-
index - 小于字符串长度的0到1之间的整数;如果未指定,则默认为0。
charCodeAt() - 返回值
返回一个数字,该数字指示给定索引处字符的Unicode值。如果给定的索引不小于字符串长度0到1,则返回NaN。
charCodeAt() - 示例
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