By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

working word counter

P: n/a
After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. I post it here to benefit
others that might want this in the future and so that if I ever lose
my copy I can come back here to find it :) Some other scripts that I
used for inspiration failed when confronted with whitespace before the
string or miscalculated when encountering linefeeds and other
non-space spaces, so I made mine better. Definition of words for this
exercise is contiguous groups of characters separated by whitespace.
Maybe it will even be useful to somebody besides me.

Cheers!
--David

<html>
<!--Generated 02/12/04-->
<!--modified 02/15/04-->
<!--name of this file: "wordCounter.html" -->

<head>
<title>Word Counter</title>
<SCRIPT LANGUAGE="JavaScript">

function trim(data)
{
while (/\s/.test(data.charAt(0)))
{data=data.substring(1,data.length);}
while (/\s/.test(data.charAt(data.length-1)))
{data=data.substring(0,(data.length-1));}
return data;
}

function countWords()
{
var input, wordList, output;
input=document.forms[0].elements[0].value;
input=trim(input);
wordList=input.split(/\s+/g);
output=wordList.length;
document.forms[0].elements[0].focus();
return output;
}

</SCRIPT>
</head>

<body bgcolor="#666666"
text="#00ff00"
link="#ffffcc"
vlink="#ffffcc">

Enter or paste text into the box below and press <b>count</b> to see
the total number of words.
<br>
<br>
<form onSubmit="return false">
<center>
<textarea rows="30" cols="100"></textarea>
<br>
<br>
<button onClick="alert('total words = ' + countWords())"
class="button">count</button>
</center>
</form>

</body>
</html>
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
David wrote:
After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. I post it here to benefit
others that might want this in the future and so that if I ever lose
my copy I can come back here to find it :) Some other scripts that I
used for inspiration failed when confronted with whitespace before the
string or miscalculated when encountering linefeeds and other
non-space spaces, so I made mine better. Definition of words for this
exercise is contiguous groups of characters separated by whitespace.
Maybe it will even be useful to somebody besides me.

Cheers!
--David

function trim(data)
{
while (/\s/.test(data.charAt(0)))
{data=data.substring(1,data.length);}
while (/\s/.test(data.charAt(data.length-1)))
{data=data.substring(0,(data.length-1));}
return data;
}
trim() would be better implemented as:

String.prototype.LTrim = new Function("return this.replace(/^\\s+/,'')")
String.prototype.RTrim = new Function("return this.replace(/\\s+$/,'')")
String.prototype.Trim = new Function("return
this.replace(/^\\s+|\\s+$/g,'')")

as shown in the FAQ at <url: http://jibbering.com/faq/#FAQ4_16 />
function countWords()
{
var input, wordList, output;
input=document.forms[0].elements[0].value;
input=trim(input);


Using the Trim() method outlined above, this would become:

input = input.Trim();

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #2

P: n/a
JRS: In article <67**************************@posting.google.com >, seen
in news:comp.lang.javascript, David <da***********@mindspring.com>
posted at Sun, 15 Feb 2004 23:51:19 :-
After getting some help working through my bugs, I have what seems to
be a robust, working word counter script. <!--Generated 02/12/04-->
this is an international newsgroup; please use internationally-
comprehensible notation. The above, in most countries, is 2nd December
2004. The correct form is 2004-02-12; or put the month as Feb and use
YYYY.
input=trim(input);
wordList=input.split(/\s+/g);
output=wordList.length;


What purpose does trim serve here? and /g?

I find
Q = X.replace(/\S+/g, 'a').replace(/\s+/g, '').length
to be significantly faster than
Q = X.split(/\s+/).length
for strings with many words. The latter creates many Objects.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.