获取字符串长度

1.

//字符串长度-中文和全角符号为1,英文、数字和半角为0.5
var getLength = function(str, shortUrl) {
    if (true == shortUrl) {
        // 一个URL当作十个字长度计算
        return Math.ceil(str.replace(/((news|telnet|nttp|file|http|ftp|https):\/\/){1}(([-A-Za-z0-9]+(\.[-A-Za-z0-9]+)*(\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\.[0-9]{1,3}){3}))(:[0-9]*)?(\/[-A-Za-z0-9_\$\.\+\!\*\(\),;:@&=\?\/~\#\%]*)*/ig, 'xxxxxxxxxxxxxxxxxxxx')
                            .replace(/^\s+|\s+$/ig,'').replace(/[^\x00-\xff]/ig,'xx').length/2);
    } else {
        return Math.ceil(str.replace(/^\s+|\s+$/ig,'').replace(/[^\x00-\xff]/ig,'xx').length/2);
    }
};

2.

var subStr = function (str, len) {
    if(!str) { return ''; }
        len = len > 0 ? len*2 : 280;
    var count = 0,    //计数:中文2字节,英文1字节
        temp = '';  //临时字符串
    for (var i = 0;i < str.length;i ++) {
        if (str.charCodeAt(i) > 255) {
            count += 2;
        } else {
            count ++;
        }
        //如果增加计数后长度大于限定长度,就直接返回临时字符串
        if(count > len) { return temp; }
        //将当前内容加到临时字符串
         temp += str.charAt(i);
    }
    return str;
};

3.

function countCharacters(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 @ 2013-11-28 16:30  幻星宇  阅读(482)  评论(0编辑  收藏  举报