PDA

View Full Version : Counting the Occurances of a Letter in a String


admin
05-30-2003, 07:44 AM
Suppose you had a string and wanted to count how many times a certain letter occured in that string...

<%
dim iCount
dim iStringLen
dim iInString
dim sString

iCount = 0
sString = "Asp is Fun"

For iStringLen = 1 to len(sString)
'the Len() function calculates the length of the string. In this case Len = 10

InString = Instr(iStringLen , sString,"s")
'the Instr() function finds the first occurance of the letter s, starting at iStringLen, but counts from the beginnig of the string.

sString= right(sString,len(sString)-iInString)
'the characters that have already been checked must be discarded

If iInString <> 0 Then
icount = icount + 1
End If

Next
response.write icount
%>

The result would be 2.

By: Andrew Donatelli