計算字符串字節數


new function(s){ 

     if(!arguments.length||!s) return null;  

     if(""==s) return 0;     //無效代碼,因為上一句!s已經判斷過

     var l=0;

     for(var i=0;i<s.length;i++){        

         if(s.charCodeAt(i)>255) l+=2; else l+=1;  //charCodeAt()得到的是unCode碼   

     }     //漢字的unCode碼大於 255bit 就是兩個字節

     alert(l); 

}("hello world!");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM