GitHub在 GitHub中添加内容: 在线编辑

indexof ()

报告输入字符串中首次出现的指定字符串的基于零的索引。

如果查找或输入字符串不是 string 类型,那么函数会强制将值强制强制转换为 string

语法

indexof(source,lookup[,start_index[,length[,实例]]])

参数

  • source: 输入字符串。
  • lookup: 要查找的字符串。
  • start_index: 搜索开始位置。 负值将通过以下许多步骤从 的末尾偏移起始搜索位置 :abs (start_index)。 可选。
  • length: 要检查的字符位置数。 值 -1 表示长度不受限制。 可选。
  • 实例数: 实例数。 缺省值 1。 可选。

退货

lookup的基于零的索引位置。

如果在输入中找不到字符串,那么返回 -1。

For irrelevant inputs (出现 < 0 or 长度 < -1) - returns .

示例

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