Subject: Get count of specific character’s occurence in a string?
I found my answer, but it’s in ASP… I think I can port it to LotusScript more or less exactly the same…
Public Function CharCount(OrigString As String, Chars As String, Optional CaseSensitive As Boolean = False) As Long
'**********************************************
'PURPOSE: Returns Number of occurrences of a character or
'or a character sequencence within a string
'PARAMETERS:
'OrigString: String to Search in
'Chars: Character(s) to search for
'CaseSensitive (Optional): Do a case sensitive search
'Defaults to false
'RETURNS:
'Number of Occurrences of Chars in OrigString
'EXAMPLES:
'Debug.Print CharCount(“FreeVBCode.com”, “E”) – returns 3
'Debug.Print CharCount(“FreeVBCode.com”, “E”, True) – returns 0
'Debug.Print CharCount(“FreeVBCode.com”, “co”) – returns 2
‘’**********************************************
Dim lLen As Long
Dim lCharLen As Long
Dim lAns As Long
Dim sInput As String
Dim sChar As String
Dim lCtr As Long
Dim lEndOfLoop As Long
Dim bytCompareType As Byte
sInput = OrigString
If sInput = “” Then Exit Function
lLen = Len(sInput)
lCharLen = Len(Chars)
lEndOfLoop = (lLen - lCharLen) + 1
bytCompareType = IIf(CaseSensitive, vbBinaryCompare, vbTextCompare)
For lCtr = 1 To lEndOfLoop
sChar = Mid(sInput, lCtr, lCharLen)
If StrComp(sChar, Chars, bytCompareType) = 0 Then lAns = lAns + 1
Next
CharCount = lAns
End Function