Search Tools Links Login

StrCount function


Function that counts the occurrance of a given phrase in a larger string.
(I needed this function and couldn't find anything like it in MSDN for VB)

Original Author: Bjorn van den Heuvel

Inputs

cToSearch: The string to search.
cSearchPhrase: The phrase to count

Assumptions

Included the check on the length of the SearchPhrase, in order to prevent a division by zero.

Returns

The number of occurrances found.

Code

Public Function StrCount(ByVal cToSearch As String, ByVal cSearchPhrase As String) As Long
Dim nDifference As Long  ' The difference in length after the replace

' Is there anything to search?
If Len(cSearchPhrase) > 0 Then
  nDifference = Len(cToSearch) - Len(Replace(cToSearch, cSearchPhrase, ""))
  StrCount = nDifference / Len(cSearchPhrase)
Else
  StrCount = 0
End If

End Function

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 100 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.