number of times a character occurs in a string

L

libsfan01

hi all

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

regards

Marc
 
R

Randy Webb

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 == characterToCount)
{
counter++;
}
}
alert('The character ' + characterToCount + ' appears ' +
counter + ' times in the sequence:\n' + origString)
 
M

Matt Kruse

Randy said:
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 =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkjahkjahkjahkjahkjahkja";
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]"));
 
R

Randy Webb

Matt Kruse said the following on 8/12/2006 3:04 PM:
Randy said:
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 =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkjahkjahkjahkjahkjahkja";
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.
 
L

libsfan01

Thanks again Randy that worked a treat!

regards

marc


Randy said:
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 == characterToCount)
{
counter++;
}
}
alert('The character ' + characterToCount + ' appears ' +
counter + ' times in the sequence:\n' + origString)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Sat, 12 Aug 2006 14:18:48 remote, seen in
Randy Webb said:
libsfan01 said the following on 8/12/2006 1:34 PM:

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

Andrew Poulos

Matt said:
Randy said:
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 =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkjahkjahkjahkjahkjahkja";
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 =
"hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkjahkjahkjahkjahkjahkja";

alert( str.length - str.replace(/a/gi,'').length);


Andrew Poulos
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Sat, 12 Aug 2006 15:36:46 remote, seen in
Randy Webb said:
Matt Kruse said the following on 8/12/2006 3:04 PM:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top