admin
06-04-2003, 04:13 PM
Here is a FASTER version to count occurrences of a string. Uses short circuit programming and does away with the FOR/NEXT loop and all that string processing.
The big difference in speed happens when you search large strings(up to 64K)!!!
<%
Dim sString
Dim sSearch
Dim sTemp
Dim iCount
Dim iPointer
sString = "Asp is Fun"
sSearch = "s"
iPointer = 1
iCount = 0
Do
sTemp = InStr(iPointer, sString, sSearch)
If sTemp = 0 Then 'do ONE test for speed
Exit Do 'stop looping if nothing found
Else
iCount = iCount + 1 'found string counter
iPointer = sTemp + 1 'move the pointer along
End If
Loop
Response.Write iCount & " found"
%>
By Ralph H Bayer 20010905
The big difference in speed happens when you search large strings(up to 64K)!!!
<%
Dim sString
Dim sSearch
Dim sTemp
Dim iCount
Dim iPointer
sString = "Asp is Fun"
sSearch = "s"
iPointer = 1
iCount = 0
Do
sTemp = InStr(iPointer, sString, sSearch)
If sTemp = 0 Then 'do ONE test for speed
Exit Do 'stop looping if nothing found
Else
iCount = iCount + 1 'found string counter
iPointer = sTemp + 1 'move the pointer along
End If
Loop
Response.Write iCount & " found"
%>
By Ralph H Bayer 20010905