js 获得字符串字节数

用到了就记下来,获得字符串字节长度,适用于汉字跟英文混合输入

 1 function getBytesCount(str)
 2 {
 3   var bytesCount = 0;
 4   if (str != null)
 5   {
 6     for (var i = 0; i < str.length; i++)
 7     {
 8       var c = str.charAt(i);
 9           if (c.match(/[^\x00-\xff]/ig) != null) //全角
10            {
11               bytesCount += 2;
12            }
13           else
14            {
15               bytesCount += 1;
16            }
17     }
18   }
19   return bytesCount;
20 }
posted @ 2014-07-21 09:20  Mauiie_娢  阅读(394)  评论(0编辑  收藏  举报