/*
如果第二个参数 bUnicode255For1 === true,则所有字符长度为 1
否则如果字符 Unicode 编码 > 255 则长度为 2
*/
//中文unicode码 简体汉字: \u4e00-\u9fa5
function strLength(s, bUnicode255For1) {
let length = s.length
if(bUnicode255For1){
return length
}else if(length > 255){
return length*2
}else{
for(const word of s){
if(/[\u4e00-\u9fa5]/.test(word)){
length++
}
}
return length
}
}
前端[编程题]获取字符串的长度
最新推荐文章于 2024-04-23 16:28:47 发布