tbwshc

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 2012-09-17 14:30 chen11-1 阅读(293) 评论(0)  编辑  收藏


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


网站导航: