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!
Re: VB.NET 2005 - Word Counter
Do you have a .Net equivalent? This is basically all vb6 code
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....
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
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.
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
Quote:
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.
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.
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.