fairworld's life

[자바스크립트] javascript 문자열 길이 체크 본문

Computer Life/Programming Life

[자바스크립트] javascript 문자열 길이 체크

fairworld 2008. 3. 6. 10:27
/*
' ------------------------------------------------------------------
' Function    : Byte_Length(lvStr)
' Description : 해당문자열의 Byte Length
' Argument    : lvStr : 대상 문자열
' Return      :
' Remark      : 2006-11-13
' Event       : Byte_Length("한aa")
' ------------------------------------------------------------------
*/
function Byte_Length(lvStr){
    var resultSize = 0;
    if (lvStr == null) return 0;
    for(var i=0; i<lvStr.length; i++){
        var c = escape(lvStr.charAt(i));
        if(c.length == 1) resultSize ++;
        else if(c.indexOf("%u") != -1) resultSize += 2;
        else if(c.indexOf("%") != -1) resultSize += c.length/3;
    }
    return resultSize;
}