function getLength(str){
var n = str.length, c, s=0;
while(n--){
c=str.charCodeAt(n);
switch(true){
case c<=0x7F: s+=1; break;
case c<=0x7FF: s+=2; break;
case c<=0xFFFF: s+=3; break;
case c<=0x1FFFF: s+=4; break;
case c<=0x7FFFFFFFF: s+=5; break;
}
}
return s;
}
console.log(getLength('สวัสดี'))
网友评论