VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Counts the number of occurrences of a character/character string. I found one code example on the n

by Chris Huff (2 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 20th April 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Counts the number of occurrences of a character/character string. I found one code example on the net and it took 4 milliseconds to run in a

Rate Counts the number of occurrences of a character/character string. I found one code example on the n



'txtCount = shows # occurances
'txtSearch = multi-line textbox that holds the contents of what you need to scan
'txtWord = the character/character string to count.
'Look at your immediate window to see the time of running, in milliseconds.


Private Sub Command1_Click()
Debug.Print MyTime
txtCount = CharCount2(txtSearch, txtWord)
     Debug.Print MyTime
End sub

Public Function MyTime() As String
  MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." & Right(Format(Timer, "#0.00"), 2)
End Function


Public Function CharCount2(OrigString As String, Chars As String) As Long
    'get number of occurences
    
    CharCount2 = 0
    
    On Error GoTo errFunc
    
    Dim lCharLen As Long
    Dim lAns As Long
    Dim sInput As String
    Dim sChar As String
    Dim isFound As Boolean
    Dim lngStart As Long
    
    If OrigString = "" Then Exit Function
    If Chars = "" Then Exit Function
    
    sInput = OrigString
    sInput = LCase(sInput)
    sChars = LCase(Chars)
    lCharLen = Len(sChars)
    
    isFound = True
    lngStart = 1
    
    Do Until isFound = False
        x = InStr(lngStart, sInput, sChars)
        If x > 0 Then
            lAns = lAns + 1
            lngStart = x + lCharLen
        Else
            isFound = False
        End If
    Loop
    
    CharCount2 = lAns
    Exit Function
    
errFunc:
    Exit Function
End Function

Download this snippet    Add to My Saved Code

Counts the number of occurrences of a character/character string. I found one code example on the n Comments

No comments have been posted about Counts the number of occurrences of a character/character string. I found one code example on the n. Why not be the first to post a comment about Counts the number of occurrences of a character/character string. I found one code example on the n.

Post your comment

Subject:
Message:
0/1000 characters