Detecting a String

aziz rasul

Active member
Local time
Today, 22:17
Joined
Jun 26, 2000
Messages
1,935
If I have a string like: -

AB sd AS rt FR AS oi as RL as

and I want to only detect the number of times that "AS" occurs in the string, what code can I use noting that I don't "as" to be included in the count?
 
I thought there was a built in function to do that but I can't find it.

If not, you'll have to write a recursive function to add 1 to the result and recall itself with a shortened string or a reference for where to start.

If you can't find the built in function and need help, give a yell back.
 
Aziz,

Untested, but probably pretty close:

Code:
Public Function fnCountWords(TheString As String, LookFor As String) As Long
Dim ptr As Long
Dim HowMany As Long

HowMany = 0
ptr = 1

While Instr(ptr, TheString, LookFor, vbBinaryCompare) > 0
   HowMany = HowMany + 1
   ptr = Instr(ptr, TheString, LookFor, vbBinaryCompare) + 1
   Wend

fnCountWords = HowMany

End Function

Wayne
 
Aziz,

Couldn't resist the recursion.

fnCountWords("AB sd AS rt FR AS oi as RL as", "AS", 0)

Code:
Public Function fnCountWords(TheString As String, LookFor As String, FoundSoFar As Long) As Long

If Instr(TheString, LookFor, vbBinaryCompare) > 0
   Temp = fnCountWords(Mid(Instr(TheString, LookFor, vbBinaryCompare) + 1), LookFor, FoundSoFar + 1)
Else
   fnCountWords = FoundSoFar
End If

End Function

Wayne
 
fascinating wayne -

i had a look at this to see if it was right, and ended up slightly differently with

Code:
Public Function fnCountWords(TheString As String, LookFor As String) As Long
If InStr(1, TheString, LookFor) > 0 Then
   fnCountWords = fnCountWords(Mid(TheString, InStr(TheString, LookFor) + 1), LookFor) + 1
End If
End Function

Sub doit()
   Call MsgBox(fnCountWords("as as as as as", "as"))
End Sub

returns 5
 
gemma,

Very nice. I didn't give my recursive algorithm a lot of thought.
Just couldn't resist throwing in something when George said recursion.

Good job,
Wayne
 
You guys are just too fast for me. I was gonna try it at lunch.

Recursion is a lot more fun but potentially problematic, especially if the original string is large. I used to run out of stack space back on VB2/3 (can't remember which, old age setting in) when I used recursive functions.
 
yeh you can easily run out of stack space with recursion

it can be slow as well

do a fibonacci recursion

function fib(x as long) as long
if x<=1 then
fib = 1
else
fib = fib(x) + fib(x-1)
end if
end function

sub tryit
msgbox(fib(20))
end sub

will start taking a long time for not very large values
 
Gemma -

The one thing that Wayne's code did but yours doesn't is to make the distinction between upper case and lower case, which the original poster requested. They wanted to count the number of "AS" but not "as" and since Access is case insensitive in this case you can't use text compare, but either binary compare or checking for each in the character set.
 
Hi -

Works well! Suppose you wanted to look specifically for "As" or "aS" or "AS". Do you see a way to do it without an ugly strConv() mess?
? fnCountWords("as as as as AS", chr(65) & chr(83)) for "AS" still returns 5.

Bob
 
Last edited:
bob

it was real slow last night, couldnt post

i didnt realise the significance of the vbBinaryCompare setting, i was more interested in the recursive code, which wasnt working quite right as presented initially -
syntax errors and undeclared variables, which were straightforward, but the recursion didnt quite work correctly, which was a bit harder to sort out

this change would sort it - i think i had another error as well - a missing start position for the second instr within the procedure

Code:
Public Function fnCountWords(TheString As String, LookFor As String) As Long
If InStr(1, TheString, LookFor,[COLOR="Red"]vbbinarycompare[/COLOR]) > 0 Then
   fnCountWords = fnCountWords(Mid(TheString, InStr([COLOR="Red"]1,[/COLOR] TheString, LookFor,[COLOR="Red"]vbbinarycompare[/COLOR]) + 1), LookFor) + 1
End If
End Function

Sub doit()
   Call MsgBox(fnCountWords("as as as as as", "as"))
End Sub
 
Last edited:
Wow! Learned something new. Have never used the compare option.
Thanks.

Bob
 
neither have i to be honest, although i cant how understand how access by default (and clearly efficiently) can do compares ignoring capitalisation - its a neat trick
 

Users who are viewing this thread

Back
Top Bottom