Get count of specific character's occurence in a string?

Let’s say I have a string of parameters consisting of one character per parameter (no spaces).

For example: parameterString = “144333BBA2”

I want to count how many of each parameter is present in the string. I’ve been searching for a string function to do that, but I can’t find one (in Lotusscript). The only way I have found so far is by doing a loop and analysing each character and then incrementing a counter… which is, I’m sure, complicating things for nothing…

Isn’t there a function I can use to do just that?

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

Subject: RE: Get count of specific character’s occurence in a string?

Here’s the working port of that function in LotusScript for the interested:

Function CharCount(Byval OrigString As String, Byval Chars As String) As String

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



sInput = OrigString

If sInput = "" Then Exit Function

lLen = Len(sInput)

lCharLen = Len(Chars)

lEndOfLoop = (lLen - lCharLen) + 1



For lCtr = 1 To lEndOfLoop

	sChar = Mid(sInput, lCtr, lCharLen)

	If Strcompare(sChar, Chars, 4) = 0 Then lAns = lAns + 1

Next



CharCount = lAns

End Function

Just use as such:

testVar$ = “AAAABBBBCCDD”

testCount$ = CharCount(testVar,“A”)

Output would be: 4

Subject: RE: Get count of specific character’s occurence in a string?

This would actually be faster (about 3 times as fast):Function CharCount(OrigString, Chars) As Long

Dim strTemp As String

strTemp = Split(OrigString, Chars)

CharCount = Clng(Ubound(strTemp))

End Function