TypeScript - 字串 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
typescript_strings.htm
廣告

© . All rights reserved.