charCodeAt()
优质
小牛编辑
140浏览
2023-12-01
描述 (Description)
此方法返回一个数字,指示给定索引处字符的Unicode值。
Unicode代码点的范围为0到1,114,111。 前128个Unicode代码点是ASCII字符编码的直接匹配。 charCodeAt()始终返回小于65,536的值。
语法 (Syntax)
下面给出了JavaScript的charCodeAt()方法的语法。 我们可以使用CoffeeScript代码中的相同方法。
string. charCodeAt(index)
它接受表示String索引的整数值,并返回String的指定索引处存在的字符的Unicode值。 如果给定索引的值不在字符串长度的0到1之间,则返回NaN 。
例子 (Example)
以下示例演示了CoffeeScript代码中JavaScript的charCodeAt()方法的用法。 将此代码保存在名为string_charcodeat.coffee的文件中
str = "This is string"
console.log "The Unicode of the character at the index (0) is:" + str.charCodeAt 0
console.log "The Unicode of the character at the index (1) is:" + str.charCodeAt 1
console.log "The Unicode of the character at the index (2) is:" + str.charCodeAt 2
console.log "The Unicode of the character at the index (3) is:" + str.charCodeAt 3
console.log "The Unicode of the character at the index (4) is:" + str.charCodeAt 4
console.log "The Unicode of the character at the index (5) is:" + str.charCodeAt 5
打开command prompt并编译.coffee文件,如下所示。
c:\> coffee -c string_charcodeat.coffee
在编译时,它为您提供以下JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
var str;
str = "This is string";
console.log("The Unicode of the character at the index (0) is:" + str.charCodeAt(0));
console.log("The Unicode of the character at the index (1) is:" + str.charCodeAt(1));
console.log("The Unicode of the character at the index (2) is:" + str.charCodeAt(2));
console.log("The Unicode of the character at the index (3) is:" + str.charCodeAt(3));
console.log("The Unicode of the character at the index (4) is:" + str.charCodeAt(4));
console.log("The Unicode of the character at the index (5) is:" + str.charCodeAt(5));
}).call(this);
现在,再次打开command prompt并运行CoffeeScript文件,如下所示。
c:\> coffee string_charcodeat.coffee
执行时,CoffeeScript文件生成以下输出。
The Unicode of the character at the index (0) is:84
The Unicode of the character at the index (1) is:104
The Unicode of the character at the index (2) is:105
The Unicode of the character at the index (3) is:115
The Unicode of the character at the index (4) is:32
The Unicode of the character at the index (5) is:105