首页 > 代码库 > 截取字符串为20个字

截取字符串为20个字

// 截取简介字符串为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个字