ホームページ  >  記事  >  ウェブフロントエンド  >  JavaSacript_基礎知識のcharCodeAt()メソッドの使い方を詳しく解説

JavaSacript_基礎知識のcharCodeAt()メソッドの使い方を詳しく解説

WBOY
WBOYオリジナル
2016-05-16 15:56:161515ブラウズ

このメソッドは、指定されたインデックスの文字の Unicode 値を表す数値を返します。

Unicode コードポイントの範囲は 0 ~ 1114111 です。最初の 128 個の Unicode コード ポイントの ASCII 文字エンコーディングと直接一致します。 charCodeAt() は常に 65,536 未満の値を返します。
文法

string.charCodeAt(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 

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。