www.webdeveloper.com
Results 1 to 6 of 6

Thread: check string for a character

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    23

    check string for a character

    I would like to know how to get the number of occurances of a specific character within a string.

    For ex. var str = "Name;Title;Year;Author;Rating"
    How do I get the number of times that the ";" appears in that string.

    Thanks in advance.

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    A re-post of something I did a while back:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Some Title</title>
    <script type="text/javascript">
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    var letters = 'valvewxnovawihirmk;diuaesvtfwy;vebhgv';
    letters += 'yaiepumnavwastzeiqvbrswdigaovs xsounaehwno pvs';
    letters += 'iagktcouqfieswvuvwhmbaqnvalvelikvgdwaedcerg vtwvkfloevguoaewqnkmvoilzavuloia';
    function countOccurences(str,valueToFind) {
        if (typeof str == 'string') {
            return str.split(valueToFind).length - 1;
        }
    }
    alert(countOccurences(letters,';'));
    alert(countOccurences(letters,'foobario'));
    alert(countOccurences(letters,'valve'));
    var numberOfZeesInLetters = countOccurences(letters,'z');
    alert(numberOfZeesInLetters);
    alert('how many v\'s: '+ countOccurences(letters,'v'));
    var str = '';
    var i = 0;
    var alpha = alphabet.charAt(i);
    while (alpha) {
        str += alpha +" appears: "+ countOccurences(letters,alpha) +" times.\n";
        alpha = alphabet.charAt(++i);
    }
    alert(str);
    </script>
    </head>
    <body>
    <div>
    </div>
    </body>
    </html>

  3. #3
    Join Date
    Sep 2009
    Posts
    52
    The poor mans way

    PHP Code:
    var 'Name;Title;Year;Author;Rating';
    alert(s.split(';').length-1); 

  4. #4
    Join Date
    Sep 2009
    Posts
    52
    Ah I see that astupidname does the same thing as I posted. I didn't read his code. My apologies.

  5. #5
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Ah I see that astupidname does the same thing as I posted. I didn't read his code. My apologies.
    Nah don't worry, it's good you came up with the same before even seeing mine!

  6. #6
    Join Date
    Oct 2009
    Posts
    1

    I am having trouble with some formating in a web page

    When I had my web pages just fill up the whole browser window this solution worked fine but now that I am using a table so it fits a 800 wide resolution screen, the results are not quite what I wanted. I have tried a few things to no avail. What I want to do is have the bgcolor (Yep I am a novice) to change just the table background color and not the whole page. I came up with a table to change the bgcolor and fgcolor on the page, but for the life of me I can not seem to just make the main table change color without changing the whole background color. http://whytehouse.com/usa/graphic.asp . No matter what I do I just can not seem to make just the table, which is about 777 pixels wide, change color while leaving the color outside of the table white. I hope I have explained this well enough so that someone can help me with the code, be it css, JavaScript, etc. The best thing to do is look at the page and you will see the color blocks and how they change the whole page, etc.

    I have tried z-index, putting an image in the background, putting a transparent spacer gif in the table and just about anything I could think of.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles