指定された位置から1文字を返します.
stringObject.charAt(index)
charAtメソッドはindexで指定された位置の文字を返します.
なお,先頭文字の位置は0番目となります.
以下にcharAtメソッドを使用して文字列から文字を抜き出すサンプルコードを示します.
<html>
<head>
<title>charAtメソッドのサンプルコード</title>
</head>
<body>
<script type="text/javascript" language="javascript">
var str = "Snoopy";
document.write("Normal: " + str + "<br />");
document.write("0番目: " + str.charAt(0) + "<br />");
document.write("1番目: " + str.charAt(1) + "<br />");
document.write("2番目: " + str.charAt(2) + "<br />");
document.write("6番目: " + str.charAt(6) + "<br />");
</script>
</body>
</html>
サンプルコードの実行結果は以下のようになります.
Normal: Snoopy 0番目: S 1番目: n 2番目: o 6番目:
スポンサード リンク