Codefixer - ASP tutorials, resources and software
asp.netPRO “Best ASP.NET Host” – DiscountASP.NET
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Wednesday 14 May
MembersPro PayPal - ASP Membership software

ASP.NET 3.5 Hosting – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Counting the number of occurences of a substring within a string

Thanks to WebbedWonder.com for providing this code snippet.

This VBScript function will return the number of occurences of a substring within a string. The function also relies on the Len function that returns the length of a string and the Instr function that returns the position of a particular substring within a string.

Call like this: dhCountIn("This is a test", "is", False)
Returns this: 2

<%
Function dhCountIn(strText, strFind, fCaseSensitive)
    ' Determine the number of times strFind appears in strText
    ' In:
    ' strText:
    ' Input text
    ' strFind:
    ' Text to find within strText
    ' fCaseSensitive (Optional, default is False):
    ' Indicates whether the search should
    ' treat upper/lower case differences as
    ' significant.
    ' Out:
    ' Return Value:
    ' The number of times strFind appears in
    ' strText, respecting the fCaseSensitive flag.
    ' Example:
    ' dhCountIn("This is a test", "is", False) returns 2
    Dim intCount' As Integer
    Dim intPos' As Integer
    Dim intMode' As Integer
    
    ' If there's nothing to find, there surely can't be any
    ' found, so return 0.
    If Len(strFind) > 0 Then
        ' Set up the comparison mode.
        If fCaseSensitive Then
            intMode = vbBinaryCompare
        Else
            intMode = vbTextCompare
        End If
        intPos = 1
        Do
           intPos = InStr(intPos, strText, strFind, intMode)
            If intPos > 0 Then
                intCount = intCount + 1
                intPos = intPos + Len(strFind)
            End If
        Loop While intPos > 0
    Else
        intCount = 0
    End If
    dhCountIn = intCount
End Function
%>  

Here's another way to count the number of substrings within a string using the Split and UBound functions.






If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.

ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software