网络编程 
首页 > 网络编程 > 浏览文章

JavaSacript中charCodeAt()方法的使用详解

(编辑:jimmy 日期: 2025/7/5 浏览:3 次 )

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

Unicode码点范围为0到1114111。前128个Unicode码点的ASCII字符编码的直接匹配。charCodeAt()将始终返回一个值小于65,536。
语法

string.charCodeAt(index);

下面是参数的详细信息:

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

返回值:

返回一个数字,表示给定索引处的字符的Unicode值。如果给定的索引不是0和1之间的长度,返回NaN。
例子:

<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 

上一篇:js+HTML5实现canvas多种颜色渐变效果的方法
下一篇:简介JavaScript中charAt()方法的使用
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。