→
その String 中で、指定位置から指定文字列を検索し最初に現れた位置を返します。見つからなかった場合は -1 を返します。
メモ: 配列メソッドについては Array.prototype.indexOf() を参照してください。
String | String、文字列。 |
---|---|
searchValue | 検索する値を表す文字列です。
文字列が明示的に提供されなかった場合は、searchValue は "undefined" となり、この値が String の中から検索されます。 ですから、例えば 'undefined'.indexOf() は undefined が undefined という文字列の中の 0 の位置に見つかるため、0 を返します。しかし、'undefine'.indexOf() は undefined が undefine という文字列の中で見つからないため、-1 を返します。 |
fromIndex | 省略可。整数値で、検索を始める位置を表します。既定値は 0 です。
fromIndex の値が 0 より小さい場合や String.length より大きい場合は、それぞれ 0 の位置、String.length の位置から検索を始めます。 例えば、'hello world'.indexOf('o',-5) は、0 の位置から検索を始め、o が 4 の位置にあるので 4 を返します。一方、'hello world'.indexOf('o',11)(および fromIndex の値が 11 よりも大きな場合)は、11 が文字列の末尾よりも後の位置であるため、-1 を返します。 |
Number。searchValue が初めて出現した位置です。見つからなかった場合は、-1 になります。
searchValue が空文字列であった場合は奇妙な結果になります。fromIndex の値がなかった場合や、fromIndex の値が文字列の length よりも小さかった場合は、返値は fromIndex と同じになります:
D='hello world'.indexOf(''); //=0 D='hello world'.indexOf('', 0); //=0 D='hello world'.indexOf('', 3); //=3 D='hello world'.indexOf('', 8); //=8
しかし、fromIndex の値が文字列の length 以上であった場合、返値は文字列の length になります。
D='hello world'.indexOf('', 11); //=11 D='hello world'.indexOf('', 13); //=11 D='hello world'.indexOf('', 22); //=11
前者の例では、JavaScript は指定された位置の直後に空文字列を見つけているようです。後者の例では、JavaScript は検索される文字列の末尾で空文字列を見つけているようです。
文字列における文字は左から右にインデックス化されます。一番最初の文字の位置は 0 で、stringName として呼び出された文字列における一番最後の文字は stringName.length -1 です。
D='Blue Whale'.indexOf('Blue'); //=0 D='Blue Whale'.indexOf('Blute'); //=-1 D='Blue Whale'.indexOf('Whale', 0); //=5 D='Blue Whale'.indexOf('Whale', 5); //=5 D='Blue Whale'.indexOf('Whale', 7); //=-1 D='Blue Whale'.indexOf(''); //=0 D='Blue Whale'.indexOf('', 9); //=9 D='Blue Whale'.indexOf('', 10); //=10 D='Blue Whale'.indexOf('', 11); //=10
indexOf() メソッドは大文字と小文字を区別します。例えば、以下の式は -1 を返します。
D='Blue Whale'.indexOf('blue'); //=-1
0 は true と評価されず、-1 は false と評価されないことに注意してください。そのため、特定の文字列がほかの文字列に含まれているかをチェックする正確な方法は次のようになります。
D='Blue Whale'.indexOf('Blue') !== -1; //=true D='Blue Whale'.indexOf('Bloe') !== -1; //=false D=~('Blue Whale'.indexOf('Bloe')); //=0。これは間違っています
以下の例は、"Brave new world" という文字列において、与えられた値の位置を求めるために、indexOf() を使用しています。
const str = 'Brave new world'; D='Index of first w from start is ' + str.indexOf('w'); //="Index of first w from start is 8" D='Index of "new" from start is ' + str.indexOf('new'); //="Index of "new" from start is 6"
以下の例は 2 つの文字列の変数を定義しています。
それらの変数は、2 番目の文字列が大文字を含んでいることを除けば、同じ文字列を含んでいます。1 番目の console.log() メソッドは 19 を表示します。しかし、indexOf() メソッドは大文字と小文字を区別するので、"cheddar" という文字列は myCapString では見つけられません。ですから、console.log() メソッドは -1 を表示します。
const myString='brie, pepper jack, cheddar'; D='myString.indexOf("cheddar") is ' + myString.indexOf('cheddar'); //="myString.indexOf("cheddar") is 19" const myCapString='Brie, Pepper Jack, Cheddar'; D='myCapString.indexOf("cheddar") is ' + myCapString.indexOf('cheddar'); //="myCapString.indexOf("cheddar") is -1"
以下の例は、count に、str という文字列中で e という文字が出現する回数を設定します。
const str = 'To be, or not to be, that is the question.'; let count = 0; let position = str.indexOf('e'); while (position !== -1) { count++ position = str.indexOf('e', position + 1) } D=count; //=4