Javascript 中的 charCodeAt()函数

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

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

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

charCodeAt() - 语法

string.charCodeAt(index);

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

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

来源:LearnFk无涯教程网

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 

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

技术教程推荐

深入拆解Tomcat & Jetty -〔李号双〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

分布式协议与算法实战 -〔韩健〕

视觉笔记入门课 -〔高伟〕

小马哥讲Spring AOP编程思想 -〔小马哥〕

Redis源码剖析与实战 -〔蒋德钧〕

零基础入门Spark -〔吴磊〕

深入拆解消息队列47讲 -〔许文强〕

云时代的JVM原理与实战 -〔康杨〕

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