午后星期午

JS计算包含英文与汉字的字符串长度(一个汉字=2个字节)

function countbCharacters(str)
{
    var totalCount = 0;
    for (var i=0; i<str.length; i++)
    {
        var c = str.charCodeAt(i);
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f))
        {
             totalCount++;
        }
        else
            
             totalCount+=2;
        }
     }
    return totalCount;
}

posted on 2013-12-31 14:06 午后星期午 阅读(228) 评论(0)  编辑  收藏 所属分类: Js


只有注册用户登录后才能发表评论。


网站导航: