如何返回表示字符 Unicode 值的数字?
htmljavascriptprogramming scripts
charCodeAt() 方法返回表示给定索引处字符 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>