码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-10-27 17:09:46      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:函数   div   func   分析   main   count   turn   str   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));

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

标签:函数   div   func   分析   main   count   turn   str   indexof   

原文地址:https://www.cnblogs.com/ceneasy/p/9862236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!