// 字符串插入换行符\n
getNewline(val) {
let str = new String(val);
let bytesCount = 0;
let s = "";
for (let i = 0, n = str.length; i < n; i++) {
let c = str.charCodeAt(i);
// 统计字符串的字符长度
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
bytesCount += 1;
} else {
bytesCount += 2;
}
// 换行
s += str.charAt(i);
if (bytesCount >= 10) {
s = s + '\n';
// 重置
bytesCount = 0;
}
}
return s;
},
js每十个字符换行
最新推荐文章于 2023-04-25 14:28:25 发布