Javascript-string-charcodeat
提供:Dev Guides
JavaScript文字列-charCodeAt()メソッド
説明
このメソッドは、指定されたインデックスにある文字のUnicode値を示す数値を返します。
Unicodeコードポイントの範囲は0〜1,114,111です。 最初の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