Simple word count?

qwertyjustin
09-06-2000, 04:16 AM
Sorry to disturb ya's all 'gain, but what would be the code for counting words in a text box?


thanx

Phil
09-06-2000, 04:36 AM
Hi,
Just an initial thought, you could use the "Split" function with a space as the deliminator to return an array of the words, then the ubound of the array + 1 would give the number of words.

Dim Words() As String
Dim NoWords As Integer

Words = Split(Trim(Text1.Text), " ")
NoWords = UBound(Words) + 1

Including Trim ensures leading or trailing spaces are not counted. There may be a better way but this is my initial thought, Good Luck
Phil

qwertyjustin
09-06-2000, 07:44 AM
didn't seem to work, any simpler method?(and i need it in a label, the amount;)

Phil
09-06-2000, 10:47 AM
Ah,
Just thought of it what version of vb are you using.
Split is only available in VB6, there is a work round however which is detailed in MSDN article Q188007,

http://support.microsoft.com/support/kb/articles/Q188/0/07.ASP?LN=EN-US&SD=msdn&FR=0

you can copy a function which will do the same job as Split.

Trying to think of a simpler method, get back to you if I can.
Cheers
Phil

Eagle
09-06-2000, 11:09 AM
I don't know if this is any more simple but you could always use a loop to cycle through each character (using SelStart, SelText, and SelLength to select each character and test them one at a time) and everytime you hit a character that is a space add 1 to a counter. Then when the you get to the end of a string add 1 to the counter for the last word. I recently had to remove a file name from its path and I did this same thing, only I counted the """ marks and when I reached the last """ then I knew that was the begining of the file name. If this is confusing I might be able to clear it up with some example code. Let me know.

BillSoo
09-06-2000, 01:51 PM
try this:

s$ = textbox1.text
if len(s$) then
n%=1
i%=0
do
i%=instr(i%+1,s$," ")
if i% then
n%=n%+1
else
exit do
endif
loop
else
n%=0
endif

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

Valkyrie
09-06-2000, 04:51 PM
Words are seperated by spaces....counting the number of spaces should do it for you. If you are dealing with large text areas where formatting and proper double spacing between sentences then that would not work, but, for a simple text box you will be alright.

Later....

Quote of the moment....
"My job is so top secret even I don't know what I'm doing!"

Derek Stone
09-06-2000, 05:04 PM
Here is some really good code from a commercial application I am creating.
It not only accounts for spaces but vbTab, vbCr, and vbLf
as well (tabs, enters, and line feeds)

Here it is, use it well my son:

<font color=blue>
Dim in_word As Boolean
Dim i As Integer
Dim ch As String
Dim num_words As Integer


For i = 1 To Len(Text1.Text)
ch = Mid$(Text1.Text, i, 1)
Select Case ch
Case " ", vbTab, vbCr, vbLf
in_word = False
Case Else
If Not in_word Then
in_word = True
num_words = num_words + 1
End If
End Select
Next i

<font color=green>'Optional</font color=green>
If num_words > 1 Then
MsgBox ("There are " & num_words & " words in this document."), vbInformation + vbOKOnly, "Word Count"
ElseIf num_words = 1 Then
MsgBox ("There is 1 word in this document."), vbInformation + vbOKOnly, "Word Count"
ElseIf num_words = 0 Then
MsgBox ("There are 0 words in this document."), vbInformation + vbOKOnly, "Word Count"
End If
</font color=blue>

Good Luck
-cl

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum