|
-
Mar 13th, 2008, 06:22 AM
#1
Thread Starter
Fanatic Member
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:
Module Module1 Public Function MyWordCount(ByVal TextToBeCounted As String) As Integer Dim SpacePos As Integer ' Stores the value returned from Instring where a space char is found. Dim X As Integer ' X tells InString from which char position to start from. Dim WordCount As Integer ' How many words there are. Dim NoMore As Boolean ' Yes or No. Dim CharValue As Integer WordCount = 0 X = 1 NoMore = False TextToBeCounted = TextToBeCounted.Replace(vbCr, " ") TextToBeCounted = TextToBeCounted.Replace(vbLf, " ") If TextToBeCounted.Trim.Length > 0 Then Do While NoMore = False SpacePos = InStr(X, Trim(TextToBeCounted), " ") If SpacePos > 0 Then CharValue = Asc(TextToBeCounted.Substring(X - 1, 1)) If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then WordCount += 1 End If X = SpacePos + 1 Do While InStr(X, (TextToBeCounted.Substring(X - 1, 1)), " ") > 0 X += 1 Loop Else If X <= TextToBeCounted.Length Then CharValue = Asc(TextToBeCounted.Substring(X - 1, 1)) If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then WordCount += 1 End If End If NoMore = True End If Loop End If MyWordCount = WordCount End Function 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.
-
Mar 13th, 2008, 10:57 AM
#2
Re: VB.NET 2005 - Word Counter
Do you have a .Net equivalent? This is basically all vb6 code
-
Mar 13th, 2008, 11:02 AM
#3
Thread Starter
Fanatic Member
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....
-
Mar 13th, 2008, 11:10 AM
#4
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:
as a couple of suggestions
-
Mar 13th, 2008, 11:22 AM
#5
Thread Starter
Fanatic Member
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.
-
Mar 13th, 2008, 12:47 PM
#6
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.
-
Mar 14th, 2008, 04:42 AM
#7
Thread Starter
Fanatic Member
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.
-
May 7th, 2008, 08:11 AM
#8
Thread Starter
Fanatic Member
Re: VB.NET 2005 - Word Counter
Updated version
vb Code:
Module Module1 Public Function MyWordCount(ByVal TextToBeCounted As String) As Integer Dim SpacePos As Integer ' Stores the value returned from Instring where a space char is found. Dim X As Integer ' X tells InString from which char position to start from. Dim WordCount As Integer ' How many words there are. Dim NoMore As Boolean ' Yes or No. Dim CharValue As Integer WordCount = 0 X = 1 NoMore = False TextToBeCounted = TextToBeCounted.Replace(vbCr, " ") TextToBeCounted = TextToBeCounted.Replace(vbLf, " ") If TextToBeCounted.Trim.Length > 0 Then Do While NoMore = False SpacePos = InStr(X, Trim(TextToBeCounted), " ") If SpacePos > 0 Then CharValue = Asc(TextToBeCounted.Substring(X - 1, 1)) If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then WordCount += 1 End If X = SpacePos + 1 Do While InStr(X, (TextToBeCounted.Substring(X - 1, 1)), " ") > 0 X += 1 Loop Else If X <= TextToBeCounted.Length Then CharValue = Asc(TextToBeCounted.Substring(X - 1, 1)) If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then WordCount += 1 End If End If NoMore = True End If Loop End If Return WordCount End Function 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|