您的位置:首页 > 其它

使用indexOf()算出长字符串中包含多少个短字符串

2014-03-10 12:14 288 查看
function countInstance(mainStr,srchStr){
var count = 0;
var offset = 0;// 搜索的起始位置
do{
offset = mainStr.indexOf(srchStr,offset);
count += (offset != -1)? 1:0;
}while(offset++ != -1);
return count;

}

alert(countInstance("wwweeewwwweee",'we')); //2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐