Javascript 中的 charCodeAt()函数

首页 / JavaScript入门教程 / Javascript 中的 charCodeAt()函数

此方法返回一个数字,指示给定索引处字符的Unicode值。

Unicode码位的范围从0到1,114,111。前128个Unicode码位是ASCII字符编码的直接匹配。charCodeAt()始终返回小于65536的值。

charCodeAt() - 语法

string.charCodeAt(index);

index   -   小于字符串长度的0到1之间的整数;如果未指定,则默认为0。

无涯教程网

charCodeAt() - 返回值

返回一个数字,该数字指示给定索引处字符的Unicode值。如果给定索引不在小于字符串长度的0到1之间,则返回NaN。

charCodeAt() - 示例

<html>
   <head>
      <title>JavaScript String charCodeAt() Method</title>
   </head>
   
   <body>   
      <script type="text/javascript">
         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>

运行上面代码输出

str.charCodeAt(0) is:84
str.charCodeAt(1) is:104
str.charCodeAt(2) is:105
str.charCodeAt(3) is:115
str.charCodeAt(4) is:32
str.charCodeAt(5) is:105 

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

算法面试通关40讲 -〔覃超〕

架构实战案例解析 -〔王庆友〕

Serverless入门课 -〔蒲松洋(秦粤)〕

Web安全攻防实战 -〔王昊天〕

手把手带你写一门编程语言 -〔宫文学〕

郭东白的架构课 -〔郭东白〕

结构写作力 -〔李忠秋〕

手把手带你写一个 MiniTomcat -〔郭屹〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)