A Way to Find Multiple Occurrence In String?

margret

Well-Known Member
Licensed User
Longtime User
I need to be able to find like the 4th and 10th occurrence in a string. I looked through the docs and found First and Last occurrence, but nothing in between. I can use Regex and a few lines of code but was looking for a single function to do this with. Are there any functions or Libs I can use to do this, something like:

MyVar.IndexOf("#", 4)
MyVar.IndexOf("#", 10)
 

Ricky D

Well-Known Member
Licensed User
Longtime User
Not that I know of but you could create a sub to do it kind of like

B4X:
Sub GetWord(str As String, wordnumber As Int) As String
    Dim ret as String

    ret = ""

    Dim s() As String

    s = Regex.Split(" ",str)

    If wordnumber<=s.Length Then
        ret = s(wordnumber-1)
    End If

    Return ret
End Sub

I just tested this on my Galaxy s2 and works fine.

B4X:
GetWord("the quick brown fox", 2) 'returns quick
GetWord("the quick brown fox", 7) 'returns empty string


regards, Ricky
 
Upvote 0
Top