js cut string by bytes All In One
js cut string by bytes All In One
中文字母(双字节字符/全角字符),字节长度 + 2
英文字母(单节字符/半角字符),字节长度 + 1
js cut max string
js 计算中英文混合的字符串的字节长度/字节大小
//'abcdefghijiklmnopqrstuvwxyz'.toUpperCase();
// 'ABCDEFGHIJIKLMNOPQRSTUVWXYZ'
const dict = 'ABCDEFGHIJIKLMNOPQRSTUVWXYZabcdefghijiklmnopqrstuvwxyz1234567890_,';
const str = 'XYZ_V_CN_2D_519fd6d91f395531d7da369b4dd98fba_初版_暴漫素材_佩奇,Andy_星澈_Clickkwala代理_720x1280_12s_211109_NR';
const arr = str.split('');
let len = 0;
let temp = 0;
let name = '';
let s = '';
for (const item of arr) {
// console.log('item =', item);
if(!dict.includes(item) ) {
// 中文字母(双字节字符/全角字符),字节长度 + 2
temp += 2;
s += item;
// 最大长度 90 字符,超出 cut 剪切掉 ✅
if(temp <= 90) {
len = temp;
name = s;
} else {
break;
}
// console.log('len & name =', len, name);
} else {
// 英文字母(单节字符/半角字符),字节长度 + 1
temp += 1;
s += item;
// 最大长度 90 字符,超出 cut 剪切掉 ✅
if(temp <= 90) {
len = temp;
name = s;
} else {
break;
}
}
}
console.log('len =', len);
console.log('name =', name);
// len = 90
// name = XYZ_V_CN_2D_519fd6d91f395531d7da369b4dd98fba_初版_暴漫素材_佩奇,Andy_星澈_Clickkwala代理_7
demos
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15928211.html
未经授权禁止转载,违者必究!