サイト内検索

String メソッド一覧

スポンサー

ホーム > String > charAt

charAt

指定された位置から1文字を返します.

シンタックス

stringObject.charAt(index)
  • 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番目: 

スポンサード リンク

inserted by FC2 system