Returns the index of the Nth occurrence of the string searched for.
Returns -1 if not found.
Example:
Returns -1 if not found.
B4X:
Sub IndexOfNth(s As String, SearchFor As String, Count As Int) As Int
Dim res As Int = -1
For i = 0 To Count - 1
res = s.IndexOf2(SearchFor, res + 1)
If res = -1 Then Return -1
Next
Return res
End Sub
Example:
B4X:
Log(IndexOfNth("a,bcd,ef,gh", ",", 1)) '1
Log(IndexOfNth("a,bcd,ef,gh", ",", 2)) '5
Log(IndexOfNth("a,bcd,ef,gh", ",", 3)) '8
Log(IndexOfNth("a,bcd,ef,gh", ",", 4)) '-1