Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The number of times a character apears in a string

Status
Not open for further replies.

Giddygoat

Programmer
May 16, 2001
36
GB
I am wondering if there is a function that returns the number of times a character appears in a string.

i.e. x = UnknownFunction('do da do da', 'd');
would result in x = 4

Cna anyone help...please

John
 
I had some help here and we came up with this. I there is a more elegant solution please let me know.

function CountChar(myString, char) {

done = false;
count = 0;
pos = -1;

while (!done) {
pos++;
pos = myString.indexOf(char, pos);
if (pos == -1) {
done = true;
}
else {
done = false;
count++;
}
}

return count;
}
 
I dont know if this could help

function CountDuplicates(strStringToSearch, strFind)
{
var intStringPos = 0;
var intStringCount = 0;
while (intStringPos != -1)
{
intStringPos = strStringToSearch.indexOf(strFind, intStringPos + 1);
intStringCount++;
}
return(intStringCount);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top