Results 1 to 8 of 8

Thread: VB.NET 2005 - Word Counter

  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?

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: VB.NET 2005 - Word Counter

    Do you have a .Net equivalent? This is basically all vb6 code
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

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

    Re: VB.NET 2005 - Word Counter

    I wrote this code in VB.Net 2005.

    I had posted on VB.Net for any suggestions and I had removed alot of the VB6 with alot of .Net.

    They suggested that InStr stay as it would be to much hassel to change over.

    So....
    Lerroy

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

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: VB.NET 2005 - Word Counter

    Most of it still is vb6:
    Code:
    TextToBeCounted = TextToBeCounted.Replace(vbCr, " ")
    TextToBeCounted = TextToBeCounted.Replace(vbLf, " ")
    could be:
    Code:
    TextToBeCounted = TextToBeCounted.Replace(Environment.NewLine, " ")
    and
    Code:
    MyWordCount = WordCount
    would be:
    Code:
    Return WordCount
    as a couple of suggestions
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5

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

    Re: VB.NET 2005 - Word Counter

    This is becoming a topic for the .Net Forum so if you are a moderator im sorry!

    I have replaced MyWordcount = WordCount with Return WOrdCount (thank you).

    I tried your other suggestion, and it does not work.
    Lerroy

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

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: VB.NET 2005 - Word Counter

    Environment.NewLine is vbCr & vbLf

    Which is carriage return + line feed, so for you to replace all carriage returns with a space, then replace all line feeds with a space would create two empty spaces when you can instead replace both with a single space by using Environment.NewLine

    For more information:
    http://msdn2.microsoft.com/en-us/lib...ne(VS.80).aspx
    NewLine can be used in conjunction with language-specific newline support such as the escape characters '\r' and '\n' in Microsoft C# and C/C++, or vbCrLf in Microsoft Visual Basic.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7

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

    Re: VB.NET 2005 - Word Counter

    With my Program at the moment, if you put in this text into the text box for example.

    A A
    A A

    Counts 4 words, if I put in what you are suggesting, it only counts 3 words.
    Lerroy

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

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  8. #8

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

    Re: VB.NET 2005 - Word Counter

    Updated version

    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.  
    17.         If TextToBeCounted.Trim.Length > 0 Then
    18.             Do While NoMore = False
    19.                 SpacePos = InStr(X, Trim(TextToBeCounted), " ")
    20.                 If SpacePos > 0 Then
    21.                     CharValue = Asc(TextToBeCounted.Substring(X - 1, 1))
    22.                     If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then
    23.                         WordCount += 1
    24.                     End If
    25.                     X = SpacePos + 1
    26.                     Do While InStr(X, (TextToBeCounted.Substring(X - 1, 1)), " ") > 0
    27.                         X += 1
    28.                     Loop
    29.                 Else
    30.                     If X <= TextToBeCounted.Length Then
    31.                         CharValue = Asc(TextToBeCounted.Substring(X - 1, 1))
    32.                         If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then
    33.                             WordCount += 1
    34.                         End If
    35.                     End If
    36.                     NoMore = True
    37.                 End If
    38.  
    39.             Loop
    40.  
    41.         End If
    42.         Return WordCount
    43.     End Function
    44. End Module

    Edit: Sorry, think I have put up the wrong updated version... Oh dear um... give me a few mins to find my latest. Sorry for spamming.
    Last edited by Lerroy_Jenkins; May 7th, 2008 at 08:16 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