码迷,mamicode.com
首页 > 其他好文 > 详细

截取字符串为20个字

时间:2017-01-04 10:40:02      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:ret   color   i++   pre   class   ring   profile   sub   截取字符串   

// 截取简介字符串为20个字
    getProfileCharCode =(val) => {
        let newVal = ‘‘;
        let zLen = 0;
        let eLen = 0;
        if(val) {
            for (let i = 0; i < val.length; i++) {
                let a = val.charAt(i);
                if (a.match(/[^\x00-\xff]/ig) != null)
                {
                    zLen += 2;
                }
                else
                {
                    eLen += 1;
                }
            }

            if(((zLen / 2) + eLen) >= 20) {
                newVal = val.substring(0, 20) + ‘...‘;
            }else {
                newVal = val;
            }
        }

        return newVal;

    }

 

截取字符串为20个字

标签:ret   color   i++   pre   class   ring   profile   sub   截取字符串   

原文地址:http://www.cnblogs.com/lqcdsns/p/6247444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!