js获取字符长度

function Len(str) {
            var i, sum;
            sum = 0;
            for (i = 0; i < str.length; i++) {
                if ((str.charCodeAt(i) >= 0) && (str.charCodeAt(i) <= 255))//如果是中文一个字为两个字节
                    sum = sum + 1;
                else
                    sum = sum + 2;
            }
            return sum;
        }

posted @ 2013-05-19 12:08  BicycleBoy  阅读(200)  评论(0编辑  收藏  举报