PDA

Click to See Complete Forum and Search --> : counting instance of a string in a string


Kagey
Mar 11th, 2001, 04:46 PM
In VbScript (ASP usage), can anyone tell me how i would check to see how many times a string occurs in another string?

Thanks for your time, patience, and money.

Mark Sreeves
Mar 12th, 2001, 05:27 AM
<%@ Language=VBScript %>
<%option explicit%>
<HTML>

<BODY>
<%

dim strTemp
strTemp = "big bad pig big hod bad door fig"
response.write strTemp & "<BR>"

response.write oc(strTemp,"bad")



Function oc(strIn, strTest)
Dim i
Dim j
Dim l
l = Len(strTest)

i = InStr(strIn, strTest)
While i > 0
j = j + 1
strIn = Mid(strIn, i + l)

i = InStr(strIn, strTest)
Wend

oc = j

End Function



%>

</BODY>
</HTML>

Active
Mar 12th, 2001, 05:39 AM
Changing Marks code..
to a simpler way...

<%@ Language=VBScript %>
<%option explicit%>
<HTML>

<BODY>
<%

dim strTemp,myArray
strTemp = "big bad pig big hod bad door fig"
response.write strTemp & "<BR>"
myArray = Split(strTemp,"bad",-1,1)
response.write Ubound(myArray)
%>

</BODY>
</HTML>

Mark Sreeves
Mar 12th, 2001, 05:54 AM
That's neat Active!

Kagey
Mar 12th, 2001, 07:48 PM
you guys rule. thanks a lot!