描述
此方法返回一個數字,指示給定索引處字符的 Unicode 值。
Unicode 代碼點的範圍從 0 到 1,114,111。前 128 個 Unicode 代碼點是 ASCII 字符編碼的直接匹配。charCodeAt()始終返回小於 65,536 的值。
用法
下麵給出的是 JavaScript 的 charCodeAt() 方法的語法。我們可以使用 CoffeeScript 代碼中的相同方法。
string. charCodeAt(index)
它接受一個表示字符串索引的整數值,並返回字符串的指定索引處存在的字符的 Unicode 值。它返回NaN如果給定的索引不小於字符串長度的 0 到 1。
示例
下麵的例子演示了使用charCodeAt()CoffeeScript 代碼中的 JavaScript 方法。將此代碼保存在具有名稱的文件中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
相關用法
- CoffeeScript String charAt()用法及代碼示例
- CoffeeScript String concat()用法及代碼示例
- CoffeeScript String slice()用法及代碼示例
- CoffeeScript String toUpperCase()用法及代碼示例
- CoffeeScript String toLocaleUpperCase()用法及代碼示例
- CoffeeScript String match()用法及代碼示例
- CoffeeScript String lastIndexOf()用法及代碼示例
- CoffeeScript String substr()用法及代碼示例
- CoffeeScript String split()用法及代碼示例
- CoffeeScript String toLowerCase()用法及代碼示例
- CoffeeScript String search()用法及代碼示例
- CoffeeScript String localeCompare()用法及代碼示例
- CoffeeScript String toLocaleLowerCase()用法及代碼示例
- CoffeeScript String indexOf()用法及代碼示例
- CoffeeScript Date setHours()用法及代碼示例
- CoffeeScript Date setUTCFullYear()用法及代碼示例
- CoffeeScript Date getDay()用法及代碼示例
- CoffeeScript Date getDate()用法及代碼示例
- CoffeeScript Date getYear()用法及代碼示例
- CoffeeScript Math pow()用法及代碼示例
注:本文由純淨天空篩選整理自 CoffeeScript String - charCodeAt()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。