通过js来获取包含中文字符的字符串的长度的方法

来源:互联网 发布:无锡网店美工培训 编辑:程序博客网 时间:2024/04/30 20:55
function  getLen( str) {
  var totallength=0;
 
  for (var i=0;i<str.length;i++)
  {
   var intCode=str.charCodeAt(i);
 
   if (intCode>=0&&intCode<=128) {
    totallength=totallength+1; //非中文单个字符长度加 1
   }
   else {
    totallength=totallength+2; //中文字符长度则加 2
   }
  } //end for
 
  return totallength;
 
}
 
原创粉丝点击