javascript计算字符串的字节长度

String.prototype.byteLen = function(){
var len = 0,
i = this.length;
while(i--)
{
  len += (this.charCodeAt(i)>255 ? 2 : 1);
}
  return len;
};

  ("测试").byteLen(); //会返回4

  ("aaa").byteLen(); //会返回3

 

posted @ 2013-07-26 15:07  悟寰轩-叶秋  阅读(226)  评论(0编辑  收藏  举报