Get number of words from string

Hewlett-packard / Hp pavilion notebook pc
February 28, 2009 at 12:07:11
Specs: Microsoft Windows XP Home Edition, 1.5 GHz / 495 MB
How can I get the number of words (instead of characters) from a string that's contained inside a textbox in VBScript?

I used len and got 68 char but I have no idea how to get the # of words instead.

I would also need to show the answer in the span block id=answer1.

Please help.

The code is below.

<script language="vbscript">
sub fred
msgbox "john_val=" +cstr(john_len)
end sub


<form name="alice">

The string john = <input type="text" size="60" name="john" value="this is a string for the purpose of playing with string coding tools">

<li><span id="answer1">this is the contents of the span block with id answer1</span>
<li><span id="answer2">this is the contents of the span block with id answer2</span>


<input type="button" value="GO" onClick="fred">

See More: Get number of words from string

Report •

February 28, 2009 at 22:42:47
I haven't used VBS before, but if it has the function split, then you could use it to find the number of words in a string. Here's a VB6 example:
Dim s As String
Dim w As Variant

s = "this is a string of words."
w = Split(s, Chr(32))
For i = 0 To UBound(w)
   Me.Print w(i) + vbCrLf
Next i

I'm not sure how one would go about using VBS to set data. In Javascript, it could be done using something similar to below.

function setTxt(txt) {
   var d = document.getElementById("answer1");
   d.innerHTML = txt;

Not really what you need, but hopefully, it's close enough to get you there. Good luck.

Report •
Related Solutions

Ask Question