Results 1 to 17 of 17

Thread: [RESOLVED] [2005] Word Counter - Any improvments?

Hybrid View

  1. #1
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Word Counter - Any improvments?

    Code:
        Function CountWords(ByVal Text As String) As Long
            Dim re As New RegularExpressions.Regex("\b\w+\b")
            ' the following pattern means that we're looking for a word character (\w)
            ' repeated one or more times (the + suffix), and that occurs on a word
            ' boundary (leading and trailing \b sequences)
            ' the Execute method does the search and returns a MatchCollection object
            ' which in turn exposes the Count property,
            '  i.e. the result we're interested into
            CountWords = re.Matches(Text).Count
        End Function
    I copied pasted changed the link kas provided.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] [2005] Word Counter - Any improvments?

    How 'bout this

    Code:
        Function CountWords(ByVal theText As String) As Int32
            Dim loopCTR, wordCTR As Int32
            Dim words() As String
            Dim valid As String = "abcdefghijklmnopqrstuvwxyz" 'characters that define word beginning
            Dim strip As String = ":;?/.>,<`~!@#$%^&*()-_=+[{}]|\'0123456789" & ControlChars.Quote 'strip this out
            valid &= valid.ToUpper 'upper and lower
            theText = theText.Trim 'get rid of lead/trail spaces
            For loopCTR = 0 To strip.Length - 1 'strip characters out (is strip defined correctly?)
                theText = theText.Replace(strip.Substring(loopCTR, 1), " ")
            Next
            words = theText.Trim.Split(" "c) 'split into an array
            For loopCTR = 0 To words.Length - 1
                If words(loopCTR) <> "" Then
                    If valid.IndexOf(words(loopCTR).Substring(0, 1)) <> -1 Then
                        wordCTR += 1
                    End If
                End If
            Next
        End Function
    I just threw this together, but my gut says strip may need attention.
    Last edited by dbasnett; Mar 5th, 2008 at 01:52 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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