indexof()
回報在輸入字串內,指定字串第一次出現以零為基礎的索引。
如需詳細資訊,請參閱indexof_regex()
。
語法
indexof(
字串,
匹配[,
開始[,
長度[,
發生]]])
深入瞭解 語法慣例。
參數
名稱 | 類型 | 必要 | Description |
---|---|---|---|
string | string |
✔️ | 要搜尋的來源字串。 |
匹配 | string |
✔️ | 要搜尋的字串。 |
開始 | int |
搜尋開始位置。 負值會將這個許多步驟從 字串 結尾位移開始搜尋位置: abs( start) 。 |
|
length (長度) | int |
要檢視的字元位置數目。 -1 的值表示沒有長度限制。 | |
occurrence | int |
出現次數。 預設值是 1。 |
注意
如果 字串 或 比對 不是 類型 string
,則函式會強制將其值 string
轉換成 。
傳回
比對的以零起始的索引位置。
- 如果在字串中找不到相符專案,則傳回 -1。
null
如果:- start 小於 0。
- 出現次數 小於 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 |
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應