如何返回一個數字,表示字元的 Unicode 值?
charCodeAt() 方法可返回一個數字表示給定 index 處的字元的 Unicode 值。Unicode 程式碼點範圍從 0 到 1,114,111。前 128 個 Unicode 程式碼點直接匹配於 ASCII 字元編碼。
charCodeAt(index) 支援以下引數−
- index − 0 與字串長度減 1 之間的整數;如果未指定,則預設為 0。
示例
你可以嘗試執行以下程式碼來返回一個數字表示字元的 Unicode 值−
<html> <head> <title>JavaScript String charCodeAt() Method</title> </head> <body> <script> var str = new String( "This is string" ); document.write("str.charCodeAt(0) is:" + str.charCodeAt(0)); document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1)); document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2)); document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3)); document.write("<br />str.charCodeAt(4) is:" + str.charCodeAt(4)); document.write("<br />str.charCodeAt(5) is:" + str.charCodeAt(5)); </script> </body> </html>
廣告