js统计字符串中每个字符出现的次数
1个回答
展开全部
<script type="text/javascript">
/*计算子字符串在字符中出现的次数*/
//字符串
var str1 = 'zhulinjia zh lia tjqig qgx gja xx yy xxf xxxq wqja ix';
//子串
var str2 = 'a';
alert( Tongji(str1 , str2) );
//统计
function Tongji(string , char)
{
var index = 0 , index1 = 0 , count = 0;
for(var i = 0; i < string.length && ( index1 !dao= -1 ); i++ )
{
index1 = string.indexOf(char, index);
index = index1 + 1;
count = i;
}
return count;
}
/*计算子字符串在字符中出现的次数*/
//字符串
var str1 = 'zhulinjia zh lia tjqig qgx gja xx yy xxf xxxq wqja ix';
//子串
var str2 = 'a';
alert( Tongji(str1 , str2) );
//统计
function Tongji(string , char)
{
var index = 0 , index1 = 0 , count = 0;
for(var i = 0; i < string.length && ( index1 !dao= -1 ); i++ )
{
index1 = string.indexOf(char, index);
index = index1 + 1;
count = i;
}
return count;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询