Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 11th, 2008, 02:25 AM
Authorized User
 
Join Date: Jul 2008
Location: , , India.
Posts: 18
Thanks: 0
Thanked 2 Times in 2 Posts
Default count occurrence of a character in a string

How to count the occurrence of character in a string in javascript?

thanx in advance

Reply With Quote
  #2 (permalink)  
Old September 11th, 2008, 02:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Trivial:

var theString = "Now is the time for all good men to come to!";

alert("There are " + (theString.split(/o/g).length - 1) + "letter o's in the string");

Split the string on the chosen letter and then the length of the resulting array is obviously one more than the number of occurrences of the letter.
Reply With Quote
  #3 (permalink)  
Old November 20th, 2008, 08:31 PM
Registered User
 
Join Date: Nov 2008
Location: Oradea, Bihor, Romania.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Old Pedant
 Trivial:

var theString = "Now is the time for all good men to come to!";

alert("There are " + (theString.split(/o/g).length - 1) + "letter o's in the string");

Split the string on the chosen letter and then the length of the resulting array is obviously one more than the number of occurrences of the letter.
I had the same problem and have searched and found a lot of solution, but yours is, by far, most simple.
Thank you! (registered just to say this :) )

Reply With Quote
  #4 (permalink)  
Old November 20th, 2008, 08:45 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

LOL! You are more than welcome!

My aim is to try to write SIMPLE code.
Reply With Quote
  #5 (permalink)  
Old September 22nd, 2010, 06:04 PM
Registered User
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This technique does not always work. For example, in IE7,
Code:
'ooo'.split(/o/g).length-1
returns -1, not 3.

On the other hand, the following will work:
Code:
'ooo'.replace(/[^o]/g, '').length
Reply With Quote
  #6 (permalink)  
Old September 22nd, 2010, 06:24 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Fascinating! How right you are. JS split does not work the same as Java split.

It's truly ugly.

Try doing:
Code:
alert( "ooo".split(/o/g) );
And you get a blank string.

APPARENTLY, the split *does* work, but then JS decides that any null elements in the resultant array aren't really there and consolidates them out of existence.

I have *NEVER* seen this documented anywhere!!

Okay, it's not quite as compact, but it's still pretty simple:
Code:
<script>
var s = "oooooo";
var re = /o/g;
for( var c = 0; re.exec(s); ++c );
alert(c);
</script>
Reply With Quote
  #7 (permalink)  
Old May 7th, 2012, 04:23 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Well, the easiest way....

There's no requirement that split() take a regular expression.

"ooo".split("o")-1

works just fine to get the letter count. That leads me to

String.prototype.count = function (srch)
{
return this.split(srch).length - 1;
};


I haven't done an exhaustive suite of tests, but I think it will work.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Count all occurences of a character in a string thomaz C# 2 February 9th, 2007 12:52 PM
Count character value using Stored Procedure itsurshailesh SQL Language 1 November 14th, 2006 12:31 AM
JSP: Count character in text file life4fun7 JSP Basics 1 June 7th, 2006 09:55 AM
character count of ms-word in asp.net sh.rajkumar ASP.NET 1.0 and 1.1 Professional 0 May 19th, 2006 07:24 AM
Search for Character in String Kaynor09 Excel VBA 2 April 24th, 2004 11:13 AM



All times are GMT -4. The time now is 06:22 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.