熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JavaScript計算字符串中每個字符出現次數的小例子

2013-11-15 12:07:01  來源: JSP教程 
這篇文章介紹了在JS中計算字符串中每個字符出現的次數有需要的朋友可以參考一下  

  代碼如下:

復制代碼 代碼如下:

  
function numInstring(str){
    str=strreplace(/ /ig"");
    var strArr=strsplit("");
    var result=[]beforeLengthafterLengthreg;
    for(var i=;i<strArrlength;i++){
        if(strindexOf(strArr[i])!=){
            beforeLength=strlength;
            reg=new RegExp(strArr[i]"ig");
            str=strreplace(reg"");
            afterLength=strlength;
            resultpush(strArr[i]+":"+(beforeLengthafterLength));
        }
    }
    return result;
}
var result=numInstring("This section of the site is dedicated to the JavaScript language itself the parts that are not specific to Web pages or other host");
consolelog(result);
/*result:
["T:" "h:" "i:" "s:" "e:" "c:" "o:" "n:" "f:" "d:" "a:" "J:" "v:" "r:" "p:" "l:" "g:" "u:" ":" "W:" "b:"]
*/


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20327.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.