-
this is the code i have to count words from a text field
the text field has 72 words but this code keeps returning 73, i know that i can minus 1 from the counter to give the right answer but that is cheap, please tell me if there is a easier way of doing this, i am using VB version 5.0
Option Explicit
Dim LineOfText, SingleCharacter As String
Dim i As Integer
Dim Counter As Integer
Counter = 0
For i = 1 To Len(LineOfText)
SingleCharacter = Mid$(LineOfText, i, 2)
If SingleCharacter = " " Then
Counter = Counter + 1
Else
SingleCharacter = Mid$(LineOfText, i, 1)
If SingleCharacter = " " Then
Counter = Counter + 1
i = i + 1
End If
End If
Next i
lblTotalWords.Caption = Counter
-
<?>
I find that to be reverse of what happens for me.
I get one less than the count of words. If there are 8 words I get sever so you need to add one to count and the reason you have to do that is because there is no " " at the end of the string but there is a word between the last " " and the end of the string.
You also don't need the i = i + 1 as the loop self increments.
-
<?>
I forgot..if you lhave any double spaces your count will be wrong as well.
-
so what should be done??
-
-
OH IT DOES!!!!!!
thanks a lot it works perfectly, but i had posted that earlier so i thought i might as well try to figure out what was wrong with my code
-
<?>
mykg4orce
Use the split funtion as recommended in your second post as it doesn't have that problem.