469,362 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

number of times a character occurs in a string

hi all

how do u get js to work out the number of times a given char occurs in
a given string?

regards

Marc

Aug 12 '06 #1
7 27767
libsfan01 said the following on 8/12/2006 1:34 PM:
hi all

how do u get js to work out the number of times a given char occurs in
a given string?
You count them. Seriously, there is no built in "howManyTimesItOccurs"
type function. You will have to write your own. You also have to decide
whether "M" and "m" are the same or not.

Simple start, there are other ways also that use charAt:

var origString = "My mama told me";
var characterToCount = "m";
var counter = 0;

//if you don't want "M" and "m" to count the same
//remove the .toLowerCase() from the line below

var myArray = origString.toLowerCase().split('');
for (i=0;i<myArray.length;i++)
{
if (myArray[i] == characterToCount)
{
counter++;
}
}
alert('The character ' + characterToCount + ' appears ' +
counter + ' times in the sequence:\n' + origString)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 12 '06 #2
Randy Webb wrote:
Simple start, there are other ways also that use charAt:
[...]
I don't know if this is generally faster or slower, but I think it's
cleaner:

var str =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkj ahkjahkjahkjahkjahkja";
String.prototype.count = function(match) {
var res = this.match(new RegExp(match,"g"));
if (res==null) { return 0; }
return res.length;
}
alert(str.count("a"));
alert(str.count("[Aa]"));

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 12 '06 #3
Matt Kruse said the following on 8/12/2006 3:04 PM:
Randy Webb wrote:
>Simple start, there are other ways also that use charAt:
[...]

I don't know if this is generally faster or slower, but I think it's
cleaner:
It's definitely different :)
var str =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkj ahkjahkjahkjahkjahkja";
String.prototype.count = function(match) {
var res = this.match(new RegExp(match,"g"));
if (res==null) { return 0; }
return res.length;
}
alert(str.count("a"));
alert(str.count("[Aa]"));
Is "match" a good name for a parameter? I don't normally use variable
names that are used by JS itself but just curious.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 12 '06 #4
Thanks again Randy that worked a treat!

regards

marc
Randy Webb wrote:
libsfan01 said the following on 8/12/2006 1:34 PM:
hi all

how do u get js to work out the number of times a given char occurs in
a given string?

You count them. Seriously, there is no built in "howManyTimesItOccurs"
type function. You will have to write your own. You also have to decide
whether "M" and "m" are the same or not.

Simple start, there are other ways also that use charAt:

var origString = "My mama told me";
var characterToCount = "m";
var counter = 0;

//if you don't want "M" and "m" to count the same
//remove the .toLowerCase() from the line below

var myArray = origString.toLowerCase().split('');
for (i=0;i<myArray.length;i++)
{
if (myArray[i] == characterToCount)
{
counter++;
}
}
alert('The character ' + characterToCount + ' appears ' +
counter + ' times in the sequence:\n' + origString)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 12 '06 #5
JRS: In article <SM******************************@comcast.com>, dated
Sat, 12 Aug 2006 14:18:48 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.composted :
>libsfan01 said the following on 8/12/2006 1:34 PM:
>how do u get js to work out the number of times a given char occurs in
a given string?
>var origString = "My mama told me";
var characterToCount = "m";
RE = new RegExp("[^" + characterToCount + "]", "gi")
Answer = origString.replace(RE, "").length

Omit the i to count only lower-case.

To count non-overlapping multi-length items, use something similar to
remove all occurrences and then see how much shorter it gets.

var orig = "My mama told me";
var Count = "ma";

RE = new RegExp(Count, "gi")
Answer = (orig.length - orig.replace(RE, "").length) / Count.length
Note that it counts in the string, and not in the literal which
generated it. Consider orig = "\u0033" ; Count = "3" giving 1.

Read the newsgroup FAQ.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 12 '06 #6
Matt Kruse wrote:
Randy Webb wrote:
>Simple start, there are other ways also that use charAt:
[...]

I don't know if this is generally faster or slower, but I think it's
cleaner:

var str =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkj ahkjahkjahkjahkjahkja";
String.prototype.count = function(match) {
var res = this.match(new RegExp(match,"g"));
if (res==null) { return 0; }
return res.length;
}
alert(str.count("a"));
alert(str.count("[Aa]"));
What about
var str =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkj ahkjahkjahkjahkjahkja";

alert( str.length - str.replace(/a/gi,'').length);
Andrew Poulos
Aug 12 '06 #7
JRS: In article <8d******************************@comcast.com>, dated
Sat, 12 Aug 2006 15:36:46 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.composted :
>Matt Kruse said the following on 8/12/2006 3:04 PM:
>var str =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAk jahkjahkjahkjahkjahkja";
String.prototype.count = function(match) {
var res = this.match(new RegExp(match,"g"));
if (res==null) { return 0; }
return res.length;
ISTM that return +res.length should do for those 2 lines.
>}
>alert(str.count("a"));
alert(str.count("[Aa]"));

Is "match" a good name for a parameter? I don't normally use variable
names that are used by JS itself but just curious.
It's not a good name, at least in contexts like that, if only because it
raises doubts such as that in the mind of the reader. Synonyms exist.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mr. B | last post: by
2 posts views Thread by Dan McCloud | last post: by
7 posts views Thread by P. Schmidt-Volkmar | last post: by
2 posts views Thread by karanbikash | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.