347   JS

英文字符,一个字幕一个字节
对于gbk编码,一个中文两个字节

String.prototype.gbklen = function() {  
  var len = 0;  
  for (var i=0; i<this.length; i++) { 
     if (this.charCodeAt(i)>127 || this.charCodeAt(i)==94) {  
       len += 2;  
     } else {  
       len ++;  
     }  
   }  
  return len;  
}

对于utf8编码,一个中文三个字节

String.prototype.utf8len = function() {  
  var len = 0;  
  for (var i=0; i<this.length; i++) { 
     if (this.charCodeAt(i)>127 || this.charCodeAt(i)==94) {  
       len += 3;  
     } else {  
       len ++;  
     }  
   }  
  return len;  
}



Leave a Reply

Your email address will not be published. Required fields are marked *