2005-10-20
getUTF8Len in JavaScript
function getUTF8Len(s) {
var len = 0;
for (var i = 0; i < s.length; i++) {
c = s.charCodeAt(i);
if (c <= 0x7f) {
len++;
} else if (c >= 0x80 && c <= 0x7ff) {
len+=3;
} else {
len+=2;
}
}
return len;
}
var len = 0;
for (var i = 0; i < s.length; i++) {
c = s.charCodeAt(i);
if (c <= 0x7f) {
len++;
} else if (c >= 0x80 && c <= 0x7ff) {
len+=3;
} else {
len+=2;
}
}
return len;
}