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
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Command$(count%) - I can't get a value when using the optional argument "(count%)" Unatic 3 516 08-22-2025, 11:58 AM
Last Post: Unatic
  Using And with two InStr calls CMR 3 588 04-25-2025, 08:52 PM
Last Post: CMR
  Looking for a solution to determine the thread count via API SagaraS 4 1,051 04-08-2024, 04:19 AM
Last Post: SpriggsySpriggs
  Reverse INSTR ? TerryRitchie 4 941 05-01-2023, 09:00 PM
Last Post: TerryRitchie
  INSTR bug? GTC 3 974 02-27-2023, 03:09 PM
Last Post: GTC

Forum Jump:


Users browsing this thread: 1 Guest(s)