VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



StrCount function

by Bjorn van den Heuvel (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 5.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (7 Votes)

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)

Inputs
cToSearch: The string to search. cSearchPhrase: The phrase to count
Assumes
Included the check on the length of the SearchPhrase, in order to prevent a division by zero.
Code Returns
The number of occurrances found.

Rate StrCount function

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

Download this snippet    Add to My Saved Code

StrCount function Comments

No comments have been posted about StrCount function. Why not be the first to post a comment about StrCount function.

Post your comment

Subject:
Message:
0/1000 characters