Assuming that people entering the string leave spaces after punctuation - commas, full-stops, exclamation marks, etc....

I've often had to tweak routines like this because people didn't.

Depends on how accurate you need it, though; the code below will be thrown slightly by dashes, em-dashes (debatable as to whether they're counted as words) and people typing immediately after punctuation:

e.g.

correct : "I went out last night. I had a great time, but I'm tired today! Can't wait til work ends - but that's 5 hours away" [ only the dash will skew this ] incorrect, but regularly types : "I went out last night.I had a great time,but I'm tired today!Can't wait til work ends-but that's 5 hours away" [a few words discounted because of the lack of spaces after ".,!"

Makes me wonder how the word-processors handle these, although I suspect they have "approximately" at the end ?

If you need an approximation, though, [the other] Liam's solution will do nicely.



Liam Potter wrote:

or if you really do want it as a plugin

Plugin:
(function($){
  $.fn.stringCount = function() {
            var string = this.text();
       var count = string.split(" ");
       var result = count.length;
             return result
  }
})(jQuery);

Use:
$(function(){
   alert( $("span.string").stringCount() );
});

Liam Potter wrote:
you don't need a plugin, this will do it

var string = $("span.string").text();
var count = string.split(" ");
alert(count.length);


Conrad Cheng wrote:
Hi all,
Any jquery plugin can check number of word in a string instead using of .length?...

Many thanks.

Conrad
------------------------------------------------------------------------


No virus found in this incoming message.
Checked by AVG - www.avg.com Version: 8.5.392 / Virus Database: 270.13.27/2258 - Release Date: 07/24/09 05:58:00


Reply via email to