/    Sign up×
Community /Pin to ProfileBookmark

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.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameOct 19.2009 — A re-post of something I did a while back:
[code=html]<!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>[/code]
Copy linkTweet thisAlerts:
@myrtle1908Oct 19.2009 — The poor mans way ?

[code=php]var s = 'Name;Title;Year;Author;Rating';
alert(s.split(';').length-1);[/code]
Copy linkTweet thisAlerts:
@myrtle1908Oct 19.2009 — Ah I see that [I]astupidname[/I] does the same thing as I posted. I didn't read his code. My apologies.
Copy linkTweet thisAlerts:
@astupidnameOct 19.2009 — Ah I see that [I]astupidname[/I] does the same thing as I posted. I didn't read his code. My apologies.[/QUOTE]
Nah don't worry, it's good you came up with the same before even seeing mine!
Copy linkTweet thisAlerts:
@DimNullOct 20.2009 — 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.
×

Success!

Help @Pbear74 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...