Results 1 to 16 of 16

Thread: Counting specific chars in a string

  1. #1
    Dreamlax
    Guest

    Counting specific chars in a string

    It looks like there is no function for counting the amount of a certain character(s) in a string (or I am just dumb). Right now I've got this:

    Code:
    Public Function StrCount(String1, String2) As Long
    For x = 1 To Len(String1)
     If Mid(String1, x, Len(String2)) = String2 Then StrCount = StrCount + 1
    Next x
    End Function
    But this seems to work too:
    Code:
    Public Function StrCount(String1, String2) As Long
    Dim sCount() As String
    sCount = Split(String1, String2)
    StrCount = UBound(sCount)
    End Function
    But surely there is a VB function that I missed, or an API. There's a few string API functions, but none of them seem to do anything related to this...

    Any Ideas?

  2. #2
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    No, but this will be faster.
    Code:
    Function CountCharacter(byval strText as string, _
            byval strCharsToCount, _
            bval lngCompare) as long
        Dim I              as long
        Dim lngCount as long
        Dim lngInstr   as long
        Dim L              as long
        I = 1
        L = Len(strCharsToCount)
        lf L = 0 then exit function
        Do
           'lngcompare  0 = BinaryCompare, 1=textcompare
            lngInstr = Instr(I,strText, _
                                     strCharsToCount, _
                                     lngCompare)
            If lngInstr = 0 then Exit Do
            lngCount = lngCount + 1
            I = lngInstr + L
        Loop
        CountCharacter = lngCount
    Last edited by John Yingling; Jul 5th, 2001 at 04:39 PM.

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    VB Code:
    1. Private Function InStrCount(Source As String, Search As String) As Long
    2.     InStrCount = Len(Source) - Len(Replace(Source, Search, ""))
    3. End Function

  4. #4
    gaffa
    Guest
    Or and easy, if not necessarily fast, method:
    Code:
    Count = UBound(Split(TheString, TheChar)) + 1
    - gaffa

  5. #5
    Dreamlax
    Guest
    The fastest seems to be the one which involves splitting and counting the array...

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Byte arrays are always the fastest appraoch to processing each character in a string.

    Test the functions on a string of 500,000 characters to quantify the difference, the results might shock you.

  7. #7
    Dreamlax
    Guest
    Yeah, the strings I tested were only about 7 or 8 thou, using a 500 thou character string showed that the Byte Array was actually a fair bit faster.

  8. #8
    Dreamlax
    Guest
    Oh, Nucleus, I just realised that your code doesn't work with Carriage Return Line Feeds (vbCrLf). Doesn't matter though because that's not what I'm after.

  9. #9
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Yes that is because carriage return is two consecutive characters not one, are you changing the spec on me? Ha!

  10. #10
    Dreamlax
    Guest
    No no no, but I was wondering why it didn't work when I tried a multibyte constant...

  11. #11
    gaffa
    Guest
    He, he,

    My little one liner will work for strings of any length....

    I'm actually pretty surprised that it's reasonably fast, I honestly thought ity would be as slow as a wet week, but there you go.

    - gaffa

  12. #12
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Yes the choice is easy one liner with vb where speed is not a concern using split/replace, but if speed is important then go with byte arrays.

    If you want to count multibyte strings and speed is required, just yell.

  13. #13
    Dreamlax
    Guest
    Speed is an issue, but not with multibyte strings...

  14. #14
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Ok so i was bored.

    I wrote the mutibyte code and compared replace,split and byte arrays.

    On my pc byte array solution was 3300% faster than both replace and split.

    The project is attached if it interests you
    Attached Files Attached Files

  15. #15
    Dreamlax
    Guest
    3300% eh? Not bad at all. In fact I'd say that's the best you could get...

  16. #16
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Yeah, but you have to write so much more code, which potentially introduces more bugs too. So in 99%of the cases I would go with Gaffa's one liner.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width