Results 1 to 9 of 9

Thread: Counting occurances in a string

Hybrid View

  1. #1
    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

  2. #2
    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

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