1. 程式人生 > >JS實現如何的統計一個字符串中相同的字符個數

JS實現如何的統計一個字符串中相同的字符個數

個數 ++ indexof log pan let index 字符串 string

 
 1          var string="start,stop,speed,start,speed,start,relocicty,start,start,start,start";
 2          var count=1;
 3          for (let index = 0; index < string.length; index++) {
 4             var a=string.indexOf("start",index);
 5              // console.log(a);
 6             if(a!=-1&&string.indexOf("start",index)!=string.indexOf("start",index-1)){
7 count++; 8 } 9 } 10 console.log(count);

JS實現如何的統計一個字符串中相同的字符個數