vanilla js get string's bytes length All In One
vanilla js get string's bytes length All In One
Blob
new Blob(["😀"]).size;
// 4
new Blob(["👻"]).size;
// 4
new Blob(["🇨🇳"]).size;
// 8
unicode
// 计算字符长度(中英文)
const getByteLen = (val) => {
let len = 0;
for (let i = 0; i < val.length; i++) {
let a = val.charAt(i);
if (a.match(/[^\x00-\xff]/ig) != null) {
len += 2;
} else {
len += 1;
}
}
return len;
};
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15130314.html
未经授权禁止转载,违者必究!