help word count using array in visual basic 6
need help to find code to count words inside a textbox using visual basic 6
i tried this code but there was an error
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
Dim i, l, words As Integer
str = TextBox1.Text
str = LTrim(str) 'removes leading blank spaces
str = RTrim(str) ' removes trailing blank spaces
l = str.Length
i = 0
words = 0
While (i < l)
If str(i) = " " Then
words = words + 1
i = i + 1
While str(i) = " " ' removes continuous blank spaces
i = i + 1
End While
Else
i = i + 1
End If
End While
words = words + 1 ' adds the last word
MessageBox.Show("WORDS =" & words)
End Sub
please help
Re: help word count using array in visual basic 6
Hi
Here's VB 6. forum.
I see your source code is vb .Net
Re: help word count using array in visual basic 6
is it possible if you could change it to vb6 code? :)
Re: help word count using array in visual basic 6
Well in Vb6 you might go this way:
Code:
dim st$
st=Trim$(TextBox.Text) 'remove leading and trailing blaks in one go
Dim a$()
a = Split(st, " ") 'split the text at every blank
dim words%
words = UBound(a)+1 'number of words is the number of elements resulting from the split
Re: help word count using array in visual basic 6
this is using array right? can it be done with array and for loop / if else?
hello voon
Re: help word count using array in visual basic 6
Yes. A() is the array. You'd loop AFTER splitting into the array. Give it a try.
Words is the UPPER BOUND of the array, so you'd loop from 0 to Max-1
Re: help word count using array in visual basic 6
Why would you use a for loop and if/then/else constructs, when the Split() functions delivers a nice array with all the single words of a text?
You can afterwards, as David explained, use any kind of loop to walk through the elements of the array.