README.md
December 5, 2022 ยท View on GitHub
Description
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)
More Info
cToSearch: The string to search.
cSearchPhrase: The phrase to count
Included the check on the length of the SearchPhrase, in order to prevent a division by zero.
The number of occurrances found.
| Submitted On | |
| By | Bjorn van den Heuvel |
| Level | Advanced |
| User Rating | 4.4 (31 globes from 7 users) |
| Compatibility | VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel |
| Category | String Manipulation |
| World | Visual Basic |
| Archive File |
Source 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