by [])utch[]v[]aster (18 Submissions)
Category: String Manipulation
Compatability: VB Script
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)
It used to be, when I wanted to count the number of times a charector appeared in a string, I would loop through the string letter by letter and keep a count. I have replace my old methodolgies with this one, which also allows you to search for substrings (more than 1-digit long)
Public Function CountChar(vText as String, vChar as String, Optional IgnoreCase as Boolean) as Integer
If IgnoreCase Then
vText = LCase$(vText)
vChar = LCase$(vChar)
End If
Dim L as Integer
L = Len(vText)
vText = Replace$(vText, vChar, "")
CountChar = (L - Len(vText)) / Len(vChar)
End Function