Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > number of times a character occurs in a string

Reply
Thread Tools

number of times a character occurs in a string

 
 
libsfan01
Guest
Posts: n/a
 
      08-12-2006
hi all

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

regards

Marc

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      08-12-2006
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/
 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      08-12-2006
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


 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      08-12-2006
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/
 
Reply With Quote
 
libsfan01
Guest
Posts: n/a
 
      08-12-2006
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/


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      08-12-2006
JRS: In article <(E-Mail Removed)>, dated
Sat, 12 Aug 2006 14:18:48 remote, seen in news:comp.lang.javascript,
Randy Webb <(E-Mail Removed)> posted :
>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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Andrew Poulos
Guest
Posts: n/a
 
      08-12-2006
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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      08-13-2006
JRS: In article <(E-Mail Removed)>, dated
Sat, 12 Aug 2006 15:36:46 remote, seen in news:comp.lang.javascript,
Randy Webb <(E-Mail Removed)> posted :
>Matt Kruse said the following on 8/12/2006 3:04 PM:


>> 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;


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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Occurs: Replace a character in a String Jimbo Python 2 04-11-2010 06:08 AM
How to tell if an a date occurs multiple times in an array/collection laredotornado Java 6 11-24-2009 12:34 PM
Count the number of times an element occurs in an array Jim Burgess Ruby 11 10-09-2009 08:33 AM
A string composed of a character repeated x number of times Sathyaish Java 11 04-04-2007 07:03 PM
Page uload occurs two times when redirecting mortb ASP .Net 5 04-08-2005 09:44 PM



Advertisments