[ Nexacro.String -  문자열 수 세기 ]

 

 


/*******************************************************************************
 ★ 설명
    대소문자 구별하여 문자개수 세기
 ★ Parameter
    1. sOrg   : 원래 문자열( 예 : "aaBBbbcc" )
    2. sCnt   : 개수를 셀 문자열 ( 예 : "BB" )
 ★ return
    - 성공 = 문자개수 ( 예 : 1 )
    - 실패 = -1
 ******************************************************************************/

function fn_Count(sOrg, sCnt)

{

 var i, sRet="";

 var nCnt = 0;

 

 if( fn_IsNull(sOrg) || fn_IsNull(sCnt) )  return -1;

 

 for( i = 0 ; i < sOrg.length ; i += sCnt.length )

 {

  if( sOrg.substr(i, sCnt.length) == sCnt )

   nCnt++;

 }

 

 return nCnt;

}

 

/*******************************************************************************
 ★ 설명
    대소문자 구분없이 문자개수 세기
 ★ Parameter
    1. sOrg   : 원래 문자열( 예 : "aaBBbbcc" )
    2. sCnt   : 개수를 셀 문자열 ( 예 : "BB" )
 ★ return
    - 성공 = 문자개수 ( 예 : 2 )
    - 실패 = -1
 ******************************************************************************/

function fn_CountCase(sOrg, sCnt)

{

 var i, sRet="";

 var nCnt = 0;

 

 if( fn_IsNull(sOrg) || fn_IsNull(sCnt) )  return -1;

 

 for( i = 0 ; i < sOrg.length ; i += sCnt.length )

 {

  if( sOrg.toLowerCase().substr(i, sCnt.length) == sCnt.toLowerCase() )

   nCnt++;

 }

  

 return nCnt;

}

/*******************************************************************************
 ★ 설명
    문자 전체 길이를 계산
      - 문자, 숫자, 특수문자 : 1 로 Count
      - 그외 한글/한자 : 2 로 count 되어 합산한다.
 ★ Parameter
    1. sVal   : 입력받은 문자열 ( 예 : "a1\n한韓" )
 ★ return
    - 성공 = 길이 ( 예 : 7 )
    - 실패 = -1
 ******************************************************************************/

function fn_LenB(sVal)

{

    var len = 0;

 

 if( fn_IsNull(sVal) )  return -1;

 

    for (i=0; i<sVal.length; i++)

    {

        if (sVal.charCodeAt(i) > 127)

            len += 2;

        else

            len += 1;

    }

 return len;

}

/******************************************************************************
  * Function명 : gf_getBytes()
  * 설명       : byte수를 반환한다.
  * Params     : String
  * Return     : byte수
  ******************************************************************************/

 function gf_getBytes(str) {

     if ( gf_isNull(str) ) return;

     

    var bytes = 0;

 

    for (var i=0, n=str.length; i < n; i++) {

         var oneChar = escape(str.charAt(i));

         if ( oneChar.length == 1 ) {

             bytes ++;

         } else if (oneChar.indexOf("%u") != -1) {

             bytes += 2;

         } else if (oneChar.indexOf("%") != -1) {

             bytes += oneChar.length/3;

         }

    }

 

    return bytes;

 }


/**
* 입력값의 바이트 길이를 리턴
* ex) if (getByteLength(form.title) > 100) {
*         alert("제목은 한글 50자(영문 100자) 이상 입력할 수 없습니다.");
*     }
* Author : Wonyoung Lee
*/

function getByteLength(input) {

    var byteLength = 0;

    for (var inx = 0; inx < input.value.length; inx++) {

        var oneChar = escape(input.value.charAt(inx));

        if ( oneChar.length == 1 ) {

            byteLength ++;

        } else if (oneChar.indexOf("%u") != -1) {

            byteLength += 2;

        } else if (oneChar.indexOf("%") != -1) {

            byteLength += oneChar.length/3;

        }

    }

    return byteLength;

}

 

☞  

 

☞  

 
 
 
 

  

'Nexacro-Function > String' 카테고리의 다른 글

Nexacro.String - etc  (0) 2017.01.28
Nexacro.String - 문자열 삭제.추가  (0) 2017.01.28
Nexacro.String - 문자열 치환  (0) 2017.01.28
Nexacro.String - 문자열 찾기  (0) 2017.01.28
Nexacro.String - token 추출  (0) 2017.01.28
Posted by 농부지기
,