VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Counting the number of instances of a string within another string

by Angsuman Banerji (23 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 4th February 2008
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Counting the number of instances of a string within another string

Rate Counting the number of instances of a string within another string



    Dim asItems() As String, lThisItem As Long
    On Error GoTo ErrFailed
    
    If bIgnoreCase Then
        asItems = Split(UCase$(sText), UCase$(sSearchFor))
        CountString = UBound(asItems)
    Else
        asItems = Split(sText, sSearchFor)
        CountString = UBound(asItems)
    End If
    
    If Len(sIgnoreText) Then
        'Deduct any items which contain the specified "sIgnoreText"
        For lThisItem = 0 To UBound(asItems) - 1
            If asItems(lThisItem) = sIgnoreText Then
                'Deduct this item
                CountString = CountString - 1
            End If
        Next
    End If
    
    Exit Function

ErrFailed:
    'Error occurred
    Debug.Print "Error in CountString " & Err.Description
    Debug.Assert False
    CountString = 0

End Function


Download this snippet    Add to My Saved Code

Counting the number of instances of a string within another string Comments

No comments have been posted about Counting the number of instances of a string within another string. Why not be the first to post a comment about Counting the number of instances of a string within another string.

Post your comment

Subject:
Message:
0/1000 characters