TheRambler
Programmer
Counting the occurrences of a string within another string can be done using the following equivalent functions:
Occurs(cSearchString, cStringSearched)
HowMany(cSearchString, cStringSearched)
where HowMany() is defined as
function HowMany(cSearchString, cStringSearched)
nOcurrence = 1
Do While At(cSearchString, cStringSearched, nOcurrence) > 0
nOcurrence = nOcurrence + 1
Enddo
Return(nOcurrence - 1)
(credits due to DSumZZZ)
for example, if
cString1 = "a"
cString2 = "abracadabra"
Occurs(cString1, cString2) = HowMany(cString1, cString2)
because both functions return 5, which is the number of times the letter "a" is found in the word "abracadabra"
But when searching for repeated characters, these functions might yield different results. For example, if
cString1 = "xx"
cString2 = "xxx"
Occurs(cString1, cString2) = 1
HowMany(cString1, cString2) = 2
or if
cString1 = "xox"
cString2 = "xoxoxox"
Occurs(cString1, cString2) = 2
HowMany(cString1, cString2) = 3
Beats me why OCCURS(cSearchString, cStringSearched) is not always equivalent to AT(cSearchString, cStringSearched, nOcurrence)
Which is better? Which is correct?
It's your choice, just be aware...
Occurs(cSearchString, cStringSearched)
HowMany(cSearchString, cStringSearched)
where HowMany() is defined as
function HowMany(cSearchString, cStringSearched)
nOcurrence = 1
Do While At(cSearchString, cStringSearched, nOcurrence) > 0
nOcurrence = nOcurrence + 1
Enddo
Return(nOcurrence - 1)
(credits due to DSumZZZ)
for example, if
cString1 = "a"
cString2 = "abracadabra"
Occurs(cString1, cString2) = HowMany(cString1, cString2)
because both functions return 5, which is the number of times the letter "a" is found in the word "abracadabra"
But when searching for repeated characters, these functions might yield different results. For example, if
cString1 = "xx"
cString2 = "xxx"
Occurs(cString1, cString2) = 1
HowMany(cString1, cString2) = 2
or if
cString1 = "xox"
cString2 = "xoxoxox"
Occurs(cString1, cString2) = 2
HowMany(cString1, cString2) = 3
Beats me why OCCURS(cSearchString, cStringSearched) is not always equivalent to AT(cSearchString, cStringSearched, nOcurrence)
Which is better? Which is correct?
It's your choice, just be aware...