Computing.Net > Forums > Web Development > Get number of words from string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Get number of words from string

Reply to Message Icon

Name: antgaudi
Date: February 28, 2009 at 12:07:11 Pacific
OS: Microsoft Windows XP Home Edition
CPU/Ram: 1.5 GHz / 495 MB
Product: Hewlett-packard / Hp pavilion notebook pc
Subcategory: General
Comment:

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.
Thanks.


<html>
<head>
<script language="vbscript">
<!--
sub fred
john_val=window.document.alice.john.value
john_len=len(john_val)
msgbox "john_val=" +cstr(john_len)
end sub

-->
</script>
</head>

<body>
<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">
</form>

<ol>
<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>

</ol>


<input type="button" value="GO" onClick="fred">
</body>
</html>



Sponsored Link
Ads by Google

Response Number 1
Name: shutat
Date: February 28, 2009 at 22:42:47 Pacific
Reply:

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.


0
Reply to Message Icon

Related Posts

See More


batch script to copy/dele... php calculation of dates



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Get number of words from string

number of entries in array www.computing.net/answers/webdevel/number-of-entries-in-array/3764.html

spam keywords? www.computing.net/answers/webdevel/spam-keywords/152.html

Compare 2 Lists of Words www.computing.net/answers/webdevel/compare-2-lists-of-words-/2500.html