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.
Printable View
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.
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>
The poor mans way :)
PHP Code:var s = 'Name;Title;Year;Author;Rating';
alert(s.split(';').length-1);
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!Quote:
Ah I see that astupidname does the same thing as I posted. I didn't read his code. My apologies.
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.