0
点赞
收藏
分享

微信扫一扫

javascript commons method

书写经典 2022-02-18 阅读 40


1.获取字符串的字节数


//取字符串字节长度  中文2个,英文1个

function getByteLength (str) {

     return str.replace(/[^\x00-\xff]/g,"00").length;

}


2.按字节来截取


/**

 * 根据字节截取

 * @param {Object} str 字符串

 * @param {Object} len 长度

 */

function subStringByByteLen(str,len){

      return str.replace(/([\u0391-\uffe5])/ig,'$1a').substring(0,len).replace(/([\u0391-\uffe5])a/ig,'$1');

}


举报

相关推荐

0 条评论