indexof函数分析及其计数字符串函数、正则表达式的例子及分析

indexof函数分

在一串字符串中统计指定字符串数量的函数。

var mainStr="star,star,study,study,star,hello";
var subStr="star";
function countStar(mainStr, subStr)
{
var count = 0;
var find = 0;
do
{
find = mainStr.indexOf(subStr, find);
if(find != -1)
{
count++;
find += subStr.length;
}
}while(find != -1)
return count;
}
document.write(countStar(mainStr, subStr));

猜你喜欢

转载自www.cnblogs.com/ceneasy/p/9862236.html
今日推荐