Java script
자바스크립트로 한글 길이 구하기
포니테일매니아
2010. 4. 28. 18:31
//바이트계산 함수
function calculate_msglen(msg)
{
var nbytes = 0;
for (i=0; i<msg.length; i++) {
var ch = msg.charAt(i);
if(escape(ch).length > 4) {
nbytes += 2;
} else if (ch == 'n') {
if (msg.charAt(i-1) != 'r') {
nbytes += 1;
}
} else if (ch == '<' || ch == '>') {
nbytes += 4;
} else {
nbytes += 1;
}
}
return nbytes;
}
한글은 2바이트, 영문은 1바이트
function calculate_msglen(msg)
{
var nbytes = 0;
for (i=0; i<msg.length; i++) {
var ch = msg.charAt(i);
if(escape(ch).length > 4) {
nbytes += 2;
} else if (ch == 'n') {
if (msg.charAt(i-1) != 'r') {
nbytes += 1;
}
} else if (ch == '<' || ch == '>') {
nbytes += 4;
} else {
nbytes += 1;
}
}
return nbytes;
}
한글은 2바이트, 영문은 1바이트