indexof()

入力文字列内で最初に見つかった指定文字列の 0 ベースのインデックスを報告します。

詳細については、「indexof_regex()」を参照してください。

構文

indexof(文字列,一致[,開始[,長さ[,発生]]])

構文規則について詳しく知る。

パラメーター

名前 必須 説明
string string ✔️ 検索するソース文字列です。
match string ✔️ 検索する文字列。
start int 検索の開始位置。 負の値を指定すると、開始位置が文字列の末尾から開始位置からオフセットされます。この多くの手順は start) ですabs(
length int 検査する文字位置の数。 値 -1 は、無制限を意味します。
occurrence int 出現する回数。 既定値は 1 です。

注意

string または match が 型stringでない場合、関数は値を に強制的にstringキャストします。

戻り値

一致の 0 から始まるインデックス位置。

  • 文字列に 一致 が見つからない場合は-1 を返 します
  • 次の場合にを返します null
    • start が 0 未満です。
    • occurrence が 0 未満です。
    • length が -1 未満です。

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 -1 -1 2 4 2 9 -1