indexof ()
入力ストリング内の指定されたストリングの最初のオカレンスのゼロ・ベースの索引を報告します。
検索または入力ストリングが string 型でない場合、関数は値を強制的に stringにキャストします。
構文
indexof(source,lookup[,start_index[,length[,オカレンス]]])
引数
- source: 入力ストリング。
- lookup: 検索するストリング。
- start_index: 検索開始位置。 負の値を指定すると、abs (start_index) という数のステップによって、 ソース の終わりから開始検索位置がオフセットされます。 オプション。
- length: 検査する文字位置の数。 値 -1 は、長さに制限がないことを意味します。 オプション。
- occurrence: 出現箇所の数。 デフォルトは 1 です。 オプション。
返品
lookupのゼロ・ベースの索引位置。
入力にストリングが見つからない場合は -1 を返します。
無関係な入力 (occurrence < 0 または length < -1) の場合、 nullを返します。
例
print
idx1 = indexof("abcdefg","cde") // lookup found in input string
, idx2 = indexof("abcdefg","cde",1,4) // lookup found in researched range
, idx3 = indexof("abcdefg","cde",1,2) // search starts from index 1, but stops after 2 chars, so full lookup can't be found
, idx4 = indexof("abcdefg","cde",3,4) // search starts after occurrence of lookup
, idx5 = indexof("abcdefg","cde",-5) // negative start index
, idx6 = indexof(1234567,5,1,4) // two first parameters were forcibly casted to strings "12345" and "5"
, idx7 = indexof("abcdefg","cde",2,-1) // lookup found in input string
, idx8 = indexof("abcdefgabcdefg", "cde", 1, 10, 2) // lookup found in input range
, idx9 = indexof("abcdefgabcdefg", "cde", 1, -1, 3) // the third occurrence of lookup is not in researched range
### 結果| idx1 | idx2 | idx3 | idx4 | idx5 | idx6 | idx7 | idx8 | idx9 |
|---|---|---|---|---|---|---|---|---|
| 2 | 2 | 2 | -1 | 2 | 4 | 2 | 9 | -1 |