'中文a【】,'.split('').reduce((len, it) => len+=it.charCodeAt(0).toString(16).length/2, 0)
使用js写一个计算字符串的字节数的方法
'中文a【】,'.split('').reduce((len, it) => len+=it.charCodeAt(0).toString(16).length/2, 0)
const computeLens = (s) => {
return s.split('').length
}
console.log(computeLens('aaacvsa'));
是字节数而不是字符数
const strByteLength = (str) => {
return (new TextEncoder()).encode(str).byteLength
}
strByteLength('c')// 1
strByteLength('你')// 3
strByteLength('𠮷')// 4
strByteLength('👏🏿')// 8
strByteLength('👩👩👧👧')// 25
学习不打烊,充电加油只为遇到更好的自己,365天无节假日,每天早上5点纯手工发布前端知识点(死磕自己,愉悦大家)。希望大家在这浮夸的前端圈里,保持冷静,坚持每天花20分钟来学习与思考。在这千变万化,类库层出不穷的前端,建议大家不要等到找工作时,才狂刷题,提倡每日学习!欢迎大家关注3+1开源项目!希望大家每人去学习与思考!(不要为了谁而来,要为自己而努力!)