目录

charCodeAt()

此方法返回一个数字,指示给定索引处字符的Unicode值。 Unicode代码点的范围为0到1,114,111。 前128个Unicode代码点是ASCII字符编码的直接匹配。 charCodeAt()始终返回小于65,536的值。

语法 (Syntax)

string.charCodeAt(index);

参数细节 (Argument Details)

  • index - 小于字符串长度的0到1之间的整数; 如果未指定,则默认为0。

返回值 (Return Value)

返回一个数字,指示给定索引处字符的Unicode值。 如果给定索引的值不在字符串长度的0到1之间,则返回NaN。

例子 (Example)

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));

输出 (Output)

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   
↑回到顶部↑
WIKI教程 @2018