TypeScript 字符串charCodeAt()方法
2019-01-10 16:48 更新
TypeScript 字符串charCodeAt()方法
charCodeAt()方法返回一个数字,指示给定索引处字符的Unicode值。Unicode代码点的范围为0到1,114,111。前128个Unicode代码点是ASCII字符编码的直接匹配。charCodeAt()始终返回小于65,536的值。
语法
string.charCodeAt(index);
参数详情
index - 小于字符串长度的0到1之间的整数;如果未指定,则默认为0。
返回值
返回一个数字,指示给定索引处字符的Unicode值。如果给定索引的值不在字符串长度的0到1之间,则返回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));
在编译时,它将在JavaScript中生成相同的代码。
其输出如下:
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
以上内容是否对您有帮助:

免费 AI IDE


更多建议: