QB64 Phoenix Edition
Instr to count occurrences - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Instr to count occurrences (/showthread.php?tid=3646)



Instr to count occurrences - PhilOfPerth - 04-29-2025

Is there, or could there be, a function that returns the number of occurrences of a character (or substring) in a string?
Like Instr, but instead of position, returns the times the substring occurs.
I know we can use Instr with a position number to repeat the search, but can we do this in a simpler way?


RE: Instr to count occurrences - bplus - 04-29-2025

Code: (Select All)
Option _Explicit
Print CountInstr&("Hello World", "l")
Print CountInstr&("Hello World", "o")
Print CountInstr&("Hello World", "W")
Print CountInstr&("Hello World", "a")

Function CountInstr& (s$, search$)
    Dim i, c&
    For i = 1 To Len(s$)
        If Mid$(s$, i, Len(search$)) = search$ Then c& = c& + 1
    Next
    CountInstr& = c&
End Function



RE: Instr to count occurrences - PhilOfPerth - 04-29-2025

(04-29-2025, 02:10 AM)bplus Wrote:
Code: (Select All)
Option _Explicit
Print CountInstr&("Hello World", "l")
Print CountInstr&("Hello World", "o")
Print CountInstr&("Hello World", "W")
Print CountInstr&("Hello World", "a")

Function CountInstr& (s$, search$)
    Dim i, c&
    For i = 1 To Len(s$)
        If Mid$(s$, i, Len(search$)) = search$ Then c& = c& + 1
    Next
    CountInstr& = c&
End Function

Thanks bplus.
That's much more compact than what I was using
Shame there's not an inbuilt function, for lazy bods like me though.  Smile


RE: Instr to count occurrences - SMcNeill - 04-29-2025

Try this: https://qb64phoenix.com/forum/showthread.php?tid=3629

Play around with the String.Find


RE: Instr to count occurrences - mdijkens - 04-29-2025

(04-29-2025, 02:52 AM)SMcNeill Wrote: Try this: https://qb64phoenix.com/forum/showthread.php?tid=3629

Play around with the String.Find
Also much more efficient.
Mid$ is a very expensive function to use in this scenario


RE: Instr to count occurrences - TempodiBasic - 04-29-2025

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