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。

链接:https://www.learnfk.comhttps://www.learnfk.com/javascript/string-charcodeat.html

来源:LearnFk无涯教程网

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 

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

技术教程推荐

软件测试52讲 -〔茹炳晟〕

Netty源码剖析与实战 -〔傅健〕

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

Selenium自动化测试实战 -〔郭宏志〕

Django快速开发实战 -〔吕召刚〕

说透低代码 -〔陈旭〕

人人都用得上的数字化思维课 -〔付晓岩〕

AI绘画核心技术与实战 -〔南柯〕

结构写作力 -〔李忠秋〕

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