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

按字节截取字符串

时间:2014-07-31 12:11:56      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:io   for   cti   ar   har   字符串   return   c   

按字节截取字符串,参数:字符串,截取的字符          function getStr(str, n) {                 if (lenFor(str) <= n) {                     return str;                 }                 var tmpStr = str.substr(0, n);                 var tmpCode = tmpStr.replace(/[^\x00-\xff]/g, ‘\r\n‘).split(‘‘);                 n = (tmpCode[n - 1] == ‘\r‘) ? n - 2 : n - 1;                 var l = tmpCode.slice(0, n).join(‘‘).replace(/\r\n/g, ‘*‘).length + 1;                 return tmpStr.substr(0, l)+"...";             };

            function lenFor(str) {                 var byteLen = 0, len = str.length;                 if (str) {                     for (var i = 0; i < len; i++) {                         if (str.charCodeAt(i) > 255) {                             byteLen += 2;                         }                         else {                             byteLen++;                         }                     }                     return byteLen;                 }                 else {                     return 0;                 }             }

按字节截取字符串,布布扣,bubuko.com

按字节截取字符串

标签:io   for   cti   ar   har   字符串   return   c   

原文地址:http://www.cnblogs.com/huweijun/p/3880026.html

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