-
April 4th, 2012, 09:59 PM
#1
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
-
April 4th, 2012, 10:27 PM
#2
Re: help word count using array in visual basic 6
Hi
Here's VB 6. forum.
I see your source code is vb .Net
-
April 5th, 2012, 01:18 AM
#3
Re: help word count using array in visual basic 6
is it possible if you could change it to vb6 code?
-
April 5th, 2012, 06:04 AM
#4
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
-
April 6th, 2012, 09:50 PM
#5
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
Last edited by chickenguru; April 6th, 2012 at 10:25 PM.
-
April 7th, 2012, 12:31 PM
#6
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
-
April 10th, 2012, 08:56 AM
#7
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.
Tags for this Thread
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
|