Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Instr to count occurrences
#1
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?
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
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
b = b + ...
Reply
#3
(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
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#4
Try this: https://qb64phoenix.com/forum/showthread.php?tid=3629

Play around with the String.Find
Reply
#5
(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
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#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




Users browsing this thread: 1 Guest(s)