纯js 原生JavaScript实现字符串长度截取

function cutstr(str, len) {
    var temp;
    var icount = 0;
    var patrn = /[^\x00-\xff]/;
    var strre = "";
    for (var i = 0; i < str.length; i++) {
        if (icount < len - 1) {
            temp = str.substr(i, 1);
            if (patrn.exec(temp) == null) {
                icount = icount + 1
            } else {
                icount = icount + 2
            }
            strre += temp
        } else {
            break
        }
    }
    return strre + "..."
}

 

posted @ 2016-04-06 10:09  WilliamShaw  阅读(424)  评论(0编辑  收藏  举报