Substring Count Demo

This demo illustrates how to quickly determine how many instances of a substring there are in a string. Enter the string to search and the substring and submit the form - you will be informed of the number of occurrences of the substring!


Enter a string:

Enter a string to search for in the above string:


Source Code
<%
  Function GetSubstringCount(strToSearch, strToLookFor, bolCaseSensative)
     If bolCaseSensative then
       GetSubstringCount = UBound(split(strToSearch, strToLookFor))
     Else
       GetSubstringCount = UBound(split(UCase(strToSearch), UCase(strToLookFor)))
     End If
  End Function

  Dim str, strToLookFor
  str = Request("txtString")
  strToLookFor = Request("txtToLookFor")

  Dim iCount
  iCount = GetSubstringCount(str, strToLookFor, False)

  If Len(str) > 0 then
%>

    <b>There were <%=iCount%> occurrences of
    "<%=strToLookFor%>" in "<%=str%>"</b>

    <p><hr><p>
<% End If %>

   <form method=post>

     Enter a string:<br>
     <textarea name="txtString" wrap="virtual" cols="40" rows="5"><%=str%></textarea>
     <p>
     Enter a string to search for in the above string:<br>
     <input name="txtToLookFor" type="text" value="<%=strToLookFor%>">

     <p><input type="submit" value="Submit">
   </form>
   


[Return to the FAQ]