Javascript-string-charat

提供:Dev Guides
移動先:案内検索

JavaScript文字列-charAt()メソッド

説明

charAt()は、指定されたインデックスから文字を返すメソッドです。

文字列内の文字は、左から右にインデックス付けされます。 最初の文字のインデックスは0で、* stringName、*と呼ばれる文字列の最後の文字のインデックスはstringName.length – 1です。

構文

特定のインデックスで文字を見つけるには、次の構文を使用します。

string.charAt(index);

引数の詳細

インデックス-文字列の長さよりも小さい0から1の間の整数。

戻り値

指定されたインデックスから文字を返します。

次の例を試してください。

<html>
   <head>
      <title>JavaScript String charAt() Method</title>
   </head>

   <body>
      <script type = "text/javascript">
         var str = new String( "This is string" );
         document.writeln("str.charAt(0) is:" + str.charAt(0));
         document.writeln("<br/>str.charAt(1) is:" + str.charAt(1));
         document.writeln("<br/>str.charAt(2) is:" + str.charAt(2));
         document.writeln("<br/>str.charAt(3) is:" + str.charAt(3));
         document.writeln("<br/>str.charAt(4) is:" + str.charAt(4));
         document.writeln("<br/>str.charAt(5) is:" + str.charAt(5));
      </script>
   </body>
</html>

出力

str.charAt(0) is:T
str.charAt(1) is:h
str.charAt(2) is:i
str.charAt(3) is:s
str.charAt(4) is:
str.charAt(5) is:i