Results 1 to 8 of 8

Thread: VB.NET 2005 - Word Counter

Threaded View

  1. #1

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    VB.NET 2005 - Word Counter

    Hello, this has got to be one of my most tresured pieces of code, so I thought it can't be that bad, so I decided to share it in the code bank!

    vb Code:
    1. Module Module1
    2.     Public Function MyWordCount(ByVal TextToBeCounted As String) As Integer
    3.         Dim SpacePos As Integer ' Stores the value returned from Instring where a space char is found.
    4.         Dim X As Integer ' X tells InString from which char position to start from.
    5.         Dim WordCount As Integer ' How many words there are.
    6.         Dim NoMore As Boolean ' Yes or No.
    7.         Dim CharValue As Integer
    8.  
    9.         WordCount = 0
    10.         X = 1
    11.         NoMore = False
    12.  
    13.         TextToBeCounted = TextToBeCounted.Replace(vbCr, " ")
    14.         TextToBeCounted = TextToBeCounted.Replace(vbLf, " ")
    15.  
    16.         If TextToBeCounted.Trim.Length > 0 Then
    17.             Do While NoMore = False
    18.                 SpacePos = InStr(X, Trim(TextToBeCounted), " ")
    19.                 If SpacePos > 0 Then
    20.                     CharValue = Asc(TextToBeCounted.Substring(X - 1, 1))
    21.                     If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then
    22.                         WordCount += 1
    23.                     End If
    24.                     X = SpacePos + 1
    25.                     Do While InStr(X, (TextToBeCounted.Substring(X - 1, 1)), " ") > 0
    26.                         X += 1
    27.                     Loop
    28.                 Else
    29.                     If X <= TextToBeCounted.Length Then
    30.                         CharValue = Asc(TextToBeCounted.Substring(X - 1, 1))
    31.                         If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then
    32.                             WordCount += 1
    33.                         End If
    34.                     End If
    35.                     NoMore = True
    36.                 End If
    37.  
    38.             Loop
    39.  
    40.         End If
    41.         MyWordCount = WordCount
    42.     End Function
    43. End Module

    This code is losely coupled so all you have to do is call into this function from say, the click of a button.

    I would also like to mention MrLudwig for helping me with this code!
    Last edited by Lerroy_Jenkins; May 7th, 2008 at 08:12 AM.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

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