Results 1 to 9 of 9

Thread: Counting occurances in a string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    395

    Counting occurances in a string

    I have a string that contains text. How do I count the number of occurances of one string in the other.

    e.g. How do I count how many times "word" is in "These are my word examples. My word on it."

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Counting occurances in a string

    Something as simple as the following sample may work for you:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim iTotal%, iPos%
    3. Dim strText$
    4.  
    5.     strText = "These are my word examples. My word on it."
    6.     iPos = 1
    7.     Do While iPos <= Len(strText)
    8.         If InStr(iPos, UCase(strText), "WORD") > 0 Then
    9.             iTotal = iTotal + 1
    10.             iPos = InStr(iPos, UCase(strText), "WORD") + Len("WORD")
    11.         Else
    12.             Exit Do
    13.         End If
    14.     Loop
    15.     MsgBox "Total occurences - " & iTotal
    16.  
    17. End Sub

  3. #3
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    Re: Counting occurances in a string

    Hi, one way could be to use regular expressions.

    Add a reference to Microsoft VBScript Regular Expressions 5.5 to you project and try this:

    VB Code:
    1. Dim objRegExp As VBScript_RegExp_55.RegExp
    2.    
    3.     Set objRegExp = New VBScript_RegExp_55.RegExp
    4.    
    5.     With objRegExp
    6.         .Pattern = "word"
    7.         .IgnoreCase = True
    8.         .Global = True
    9.         MsgBox .Execute("These are my word examples. My word on it.").Count
    10.     End With

    HTH

  4. #4
    Addicted Member Luke K's Avatar
    Join Date
    Jun 2004
    Location
    Perth, Australia
    Posts
    183

    Re: Counting occurances in a string

    There are two ways off the top of my head I can think of performing this task accurately. Here they are:

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim stgSearch      As String
    4.     Dim stgWordCount() As String
    5.  
    6.     stgSearch = "I wrote these words, I put my word on it!"
    7.     stgWordCount = Split(stgSearch, "word", -1)
    8.     Call MsgBox(UBound(stgWordCount))
    9.  
    10. End Sub

    OR

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim stgSearch As String
    4.     Dim intCurr   As Integer
    5.     Dim intOccur  As Integer
    6.     Dim intLen    As Integer
    7.  
    8.     stgSearch = "I wrote these words, I put my word on it!"
    9.  
    10.     intOccur = InStr(1, stgSearch, "word")
    11.     If intOccur = 0 Then
    12.         Call MsgBox("There are no occurances of " & Chr(34) & "word" & Chr(34) & _
    13.             "in that sentence")
    14.     Else
    15.         intLen = 1
    16.         While InStr(intLen, stgSearch, "word") > 0
    17.             intCurr = InStr(intLen, stgSearch, "word")
    18.             intTotal = intTotal + 1
    19.             intLen = intLen + intCurr + 1
    20.         Wend
    21.         Call MsgBox("There were " & intTotal & " occurances of " & Chr(34) & _
    22.             "word" & Chr(34) & " in that sentence")
    23.     End If
    24.  
    25.     Unload Me
    26.  
    27. End Sub
    EDIT:
    Damn, Rhino beat me to the punch

    HTH
    Last edited by Luke K; Apr 14th, 2005 at 08:18 AM.
    Artificial Intelligence At War! - The best game of its genre
    Program your own robot and watch it fight in 3d!
    Droidarena 3

    If I have been useful, please Rate My Post

    Support FireFox -
    Microsoft Visual Studio .NET Professional 2003
    Microsoft Visual Studio 6, Enterprise Edition
    Microsoft Windows XP Professional, Service Pack 2

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Counting occurances in a string

    Something like this should work
    VB Code:
    1. Private Sub Command1_Click()
    2. MsgBox "The word word appears in this string " & UBound(Split(Text1.Text, "word")) & " times."
    3. End Sub

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Counting occurances in a string

    Yet another way...

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim strtext As String, strSearch As String
    4.  
    5. strtext = UCase("These are my word examples. My word on it.")
    6. strSearch = UCase("word")
    7.  
    8. Debug.Print "Times WORD in string = "; (Len(strtext) - Len(Replace(strtext, strSearch, ""))) / Len(strSearch)
    9.  
    10. End Sub

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Counting occurances in a string

    Actually the method I posted, and most of the others, will find the "word" imbedded in other words and count those also...

    I've made some posts recently about parsing a sentence - looking for spaces and commas and other punctuation.

    If you are looking for perfect accuracy, then you need to look at each word one word at a time - that's going to require a loop through the string looking at each byte one at a time and checking for puncuation characters...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Posts
    395

    Re: Counting occurances in a string

    What fabulous examples. Thank you!

    Let me explain what I am trying to do. I have a textbox called Keywords. I have another textbox called ArticleText.

    The idea is that I enter words into the Keywords field. e.g. "All about me"

    Then you write the article. I have code that is triggered - using the Change event - that does an on-the-fly word count. But I also need an on the fly count of the Keywords.

    e.g. ArticleText="This article is about me and my friends"

    This would give a keyword count of 2 since the words about occured once and me occured once.

    So, I figured I would split the Keywords into an array.

    'Put Keywords into array
    Dim list As Variant
    Dim strSentence As String
    Dim counter As Integer
    strSentence = Keywords.Text
    strSentence = Trim(strSentence)
    list = Split(strSentence, " ")

    Now I need to do some kind of loop using the Change event so that it does a Keyword count.

    Any ideas?

    Hope this makes sense. I'm new to Visual Basic 6 but am amazed at how quickly you can developed stuff. It's my new hobby!

  9. #9
    Addicted Member Luke K's Avatar
    Join Date
    Jun 2004
    Location
    Perth, Australia
    Posts
    183

    Re: Counting occurances in a string

    If you are going to do an on the fly count a loop wouldnt be advisable, because the more the user enters the longer the loop will have to go for and it will keep building up and up.

    I suggest using one of the many Split methods that everyone has posted, using your keyword string as the delimiter parameter.

    The attatched screenshot is a working example
    HTH
    Attached Images Attached Images  
    Artificial Intelligence At War! - The best game of its genre
    Program your own robot and watch it fight in 3d!
    Droidarena 3

    If I have been useful, please Rate My Post

    Support FireFox -
    Microsoft Visual Studio .NET Professional 2003
    Microsoft Visual Studio 6, Enterprise Edition
    Microsoft Windows XP Professional, Service Pack 2

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