Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting string occurrences

Status
Not open for further replies.

TheRambler

Programmer
Jan 23, 2003
523
BO
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...

 
mlv0155,

Beats me why OCCURS(cSearchString, cStringSearched) is not always equivalent to AT(cSearchString, cStringSearched, nOcurrence)

You've raised a very good issue. It's hard to explain the reason, but intuitively the behaviour seems correct to me.

Just think about how AT() works. It returns the beginning position of the substring. The substring might contain repeated sub-substrings, but that won't affect the position of the first character.

Mike




Mike Lewis
Edinburgh, Scotland
 
Mr. Lewis,

...intuitively the behaviour seems correct to me

Which behaviour? At() or Occurs()? or both? If

cString1 = "xox" and
cString2 = "xoxoxox"

Occurs(cString1, cString2) = 2, but
At(cString1, cString2, 1) = 1
At(cString1, cString2, 2) = 3
At(cString1, cString2, 3) = 5

why Occurs() says there are 2 occurrences, but At() finds 3 of them? To me, that is not consistent, or maybe it is a peculiarity we could use for some purpose? I don't know.
 
It looks to me like OCCURS() takes the first occurrence, counts it, and continues on down the string from that point, rather than character by character.


-Dave S.-
[cheers]
Even more Fox stuff at:
 
mlv1055,

Which behaviour? At() or Occurs()? or both?

Well, I mean AT(), but, now you mention it, both of them seem intuitive to me. I've never seen it as a problem.

Mike


Mike Lewis
Edinburgh, Scotland
 
I agree with both.

OCCURS() is equivalent to:
nOccurrence = 0
do while at(cSearchString, cStringSearched) > 0
nOccurrence = nOccurrence + 1
cStringSearched = substr(cStringSearched, ;
at(cSearchString, cStringSearched) +len(cSearchString))
enddo
return(nOccurrence)

while AT() behaves like:
nOccurrence = 0
do while at(cSearchString, cStringSearched) > 0
nOccurrence = nOccurrence + 1
cStringSearched = substr(cStringSearched, ;
at(cSearchString, cStringSearched) +1)
enddo
return(nOccurrence)

If you are not aware of it, then you have no problem.
There is no need for them to tell us...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top