Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Instr to count occurrences
#6
Hi
taken from wiki INSTR example,  Big Grin  

Code: (Select All)

_Title "Find recurrence and position of a substring in a string"
Dim text$, Searched$, Count%
ReDim Where%(1 To 1)
text$ = "The cats and dogs where playing, even though dogs don't like cats."
Searched$ = "cats"
Print "Founded: "; Find(Count%, Where%(), text$, Searched$); Count%; " times"
For see% = 1 To Count% Step 1
    Print Where%(see%)
Next see%
End

Function Find (Count%, Where%(), Text$, Searched$)
    Dim findS%
    Count% = 0
    Find = 0
    If Len(Text$) = 0 Or Len(Searched$) = 0 Then Exit Function
    Do
        findS% = InStr(findS% + 1, Text$, Searched$) ' find another occurance after
        If findS% Then
            Count% = Count% + 1
            ReDim _Preserve Where%(1 To Count%)
            Where%(Count%) = findS%
        End If
    Loop Until findS% = 0
    If Count% > 0 Then Find = -1
End Function
Reply


Messages In This Thread
Instr to count occurrences - by PhilOfPerth - 04-29-2025, 01:35 AM
RE: Instr to count occurrences - by bplus - 04-29-2025, 02:10 AM
RE: Instr to count occurrences - by PhilOfPerth - 04-29-2025, 02:21 AM
RE: Instr to count occurrences - by SMcNeill - 04-29-2025, 02:52 AM
RE: Instr to count occurrences - by mdijkens - 04-29-2025, 05:09 PM
RE: Instr to count occurrences - by TempodiBasic - 04-29-2025, 11:17 PM



Users browsing this thread: 1 Guest(s)