javaScrip string (String) related

----------------------------------------- This page only records the front-end stuff about String ---------------------------------------------------------

1  // Common String 
2  var str='abcdefgannnannkkaoooa';;

1.0 Get a set of all index values ​​of a string getIndexArr(str,'a') = > [0, 7, 11, 16, 20]

1 function getIndexArr(str,targetStr){
2     var indexArr=[];
3     for(var i=0;i<str.length;i++){ 
4         if(str[i]===targetStr){ 
5             indexArr.push(i); 
6         }
7     }
8     return indexArr;
9 }

2.0 Replace all selected strings in the string with the new string, return the replaced string replace(str,'a','i') => 'I bcdefg I nnn I nnkk I ooo I'

1 function replace(str,targetStr,newStr){
2     return str.split(targetStr).join(newStr);
3 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325015344&siteId=291194637