number of times a character occurs in a string

Discussion in 'Javascript' started by libsfan01, Aug 12, 2006.

  1. libsfan01

    libsfan01 Guest

    hi all

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

    regards

    Marc
    libsfan01, Aug 12, 2006
    #1
    1. Advertising

  2. libsfan01

    Randy Webb Guest

    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)

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Aug 12, 2006
    #2
    1. Advertising

  3. libsfan01

    Matt Kruse Guest

    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 =
    "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]"));

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Aug 12, 2006
    #3
  4. libsfan01

    Randy Webb Guest

    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 =
    > "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.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Aug 12, 2006
    #4
  5. libsfan01

    libsfan01 Guest

    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 == 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/
    libsfan01, Aug 12, 2006
    #5
  6. JRS: In article <>, dated
    Sat, 12 Aug 2006 14:18:48 remote, seen in news:comp.lang.javascript,
    Randy Webb <> 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.
    Dr John Stockton, Aug 12, 2006
    #6
  7. 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 =
    > "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
    Andrew Poulos, Aug 12, 2006
    #7
  8. JRS: In article <>, dated
    Sat, 12 Aug 2006 15:36:46 remote, seen in news:comp.lang.javascript,
    Randy Webb <> posted :
    >Matt Kruse said the following on 8/12/2006 3:04 PM:


    >> var str =
    >> "hjahjkahjkahjaguyayafhajagjhajAahkjahkAjahkjahAkjahkjahkjahkjahkjahkja";
    >> 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.
    Dr John Stockton, Aug 13, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mortb
    Replies:
    5
    Views:
    410
    Brock Allen
    Apr 8, 2005
  2. Sathyaish
    Replies:
    11
    Views:
    1,657
    Daniel Pitts
    Apr 4, 2007
  3. laredotornado
    Replies:
    6
    Views:
    504
    Teraposa Lunodas
    Nov 24, 2009
  4. Jimbo
    Replies:
    2
    Views:
    415
    Patrick Maupin
    Apr 11, 2010
  5. Jim Burgess
    Replies:
    11
    Views:
    359
    Jim Burgess
    Oct 9, 2009
Loading...

Share This Page