How to count the number of occurences of [string]

How to count the number of occurences of [string] ?
where string can be a single character or a word.

I’ve made a quest, but I found nothing (I may be walking beside my shoes right now).

All I got was a loop and count character after character the number of occurances of character"x" or word “xyz”.

Anything faster exists ?

Maybe CountFields?
http://documentation.xojo.com/index.php/CountFieldsB

yes countFields -1
n=countFiledsB(text, stringToSearch)-1

but some time it will return less result (for single char in big strings) than
n=ubound(text.split(stringToSearch))
That is a little slower but solid.

If you’ve got the MBS plugin there is a function called CountOccurancesMBS

[code]dim s as string

s=“111110001110101010111”

MsgBox str(CountOccurancesMBS(s,“1”)) // 14[/code]

Paul: unfortunately, no MBS here.

Albin, Antonio: thanks for your answers.

The loop case tooks times for onlmy 12,000 characters to check (first in a select case, then in an If block).

For some reason (forgotten), in another project, I used (I do not remember, but it returned 1 or > 1) instead of InStr and does not understand why the program does not always do what I wanted. It tooks me some minutes (and a coffee) to understand my mistake. It was a case of “automatic write” (I wrote that word because… I wrote it ! Never knew why; probably a mistake / I took one word instead of another, just like a mother can use one of its childs name to call another 'till she realize her error ;-:slight_smile: This happened to Mother, sometimes).

RegEx immediately springs to my mind as a solution. You can do a regex search with the string as a pattern and simply count the matches.

When you want to count strings in huge strings (e.g. file content) you should use Memoryblocks instead of countFiledsB. Couple of Month ago there was a topic here in forum but I missed to bookmark it.

RegEx is also a solution, but it is slower then the split solution (not so slow, only slower)

please is any body here to help me ?

please how can i create a desktop program that counts the number of digits that contain the number 7 in the range 0 to 10000

First off, start with a question… not a generic plea for help

Second, show us what you have tried to do already… the purpose of this forum is to help you use XOJO to solve problems, but you have to be willing to try and figure things out for yourself as well

dim count as integer=0
dim i as integer
for i=0 to 10000
   if instr(str(i),"7")>0 then count=count+1
next i
msgbox str(count)

Looks like someone has been instructed some homework :wink: