JavaSacript中charCodeAt()方法的使用詳解
該方法返回一個(gè)數(shù)字,表示給定索引處的字符的Unicode值。
Unicode碼點(diǎn)范圍為0到1114111。前128個(gè)Unicode碼點(diǎn)的ASCII字符編碼的直接匹配。charCodeAt()將始終返回一個(gè)值小于65,536。
語法
string.charCodeAt(index);
下面是參數(shù)的詳細(xì)信息:
- index: 0和1之間小于字符串的長度的整數(shù); 如果未指定,默認(rèn)為0。
返回值:
返回一個(gè)數(shù)字,表示給定索引處的字符的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>
這將產(chǎn)生以下結(jié)果:
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
相關(guān)文章
javascript學(xué)習(xí)筆記(二)數(shù)組和對(duì)象部分
本文是學(xué)習(xí)筆記系列的第二篇,深入淺出的分別從javascript對(duì)象和數(shù)組兩個(gè)部分介紹了相關(guān)知識(shí),并附上詳細(xì)示例,非常的實(shí)用,有需要的朋友可以參考下2014-09-09
JavaScript學(xué)習(xí)筆記之基礎(chǔ)語法
本文不是零基礎(chǔ)教學(xué),請(qǐng)沒javascript基礎(chǔ)的小伙伴們先出門左拐,本人之前學(xué)習(xí)java的,所以本文主要對(duì)比下java學(xué)習(xí)javascript。2015-01-01
JavaScript起點(diǎn)(嚴(yán)格模式深度了解)
嚴(yán)格模式(Strict Mode)是ECMAScript5新增的功能,目前所有的主流瀏覽器的最新版本——包括IE10與Opera12——都支持嚴(yán)格模式,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01

