Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:String:charAt

出典: MDC



目次

[編集] 概要

文字列から指定された文字を返します。

String のメソッド
実装されたバージョン: JavaScript 1.0, NES2.0
ECMA バージョン: ECMA-262

[編集] 構文

charAt(index)

[編集] パラメータ

index 
0 と文字列の長さの間の整数。

[編集] 詳細

文字列における文字は左から右の方向にインデックス化されます。一番最初の文字のインデックスは 0 で、一番最後の文字のインデックスは、stringName を呼び出す文字列においては、stringName.length - 1 です。与える index が範囲外の場合、JavaScript は 空文字列を返します。

[編集]

[編集] 例: 文字列における異なる位置の文字を表示する

以下の例は "Brave new world" という文字列における異なる位置の文字を表示します。:

var anyString="Brave new world"

document.writeln("The character at index 0 is '" + anyString.charAt(0) + "'")
document.writeln("The character at index 1 is '" + anyString.charAt(1) + "'")
document.writeln("The character at index 2 is '" + anyString.charAt(2) + "'")
document.writeln("The character at index 3 is '" + anyString.charAt(3) + "'")
document.writeln("The character at index 4 is '" + anyString.charAt(4) + "'")
document.writeln("The character at index 999 is '" + anyString.charAt(999) + "'")

これらの行は以下のように表示します。:

The character at index 0 is 'B'
The character at index 1 is 'r'
The character at index 2 is 'a'
The character at index 3 is 'v'
The character at index 4 is 'e'
The character at index 999 is ''

[編集] 参照

indexOf, lastIndexOf, split, charCodeAt