본문 바로가기
코딩 저장용

[공통제작시] 공통코드로 쓸만한것

by 디지털프리덤 2018. 8. 17.
반응형

STRING 유틸




1. String객체가 Null이면 지정값으로 반환한다.


/**

 * String객체가 Null이면 지정값으로 반환한다.

 *

 * @param String형 객체

 * @return 변환 객체

 * @see

 */

public static String replaceNull( String value, String convValue ) {

String rslt = value;

if( rslt == null || rslt.trim().length() == 0 ) rslt = convValue;

return rslt;

}

public static String replaceNull( Object value, String convValue ) {

Object rslt = value;

if( rslt == null) rslt = convValue;

return (String)rslt;

}




2. 문자열 자르기.

   /**

     *

     * @param value : 잘라낼 문자

     * @param startIdx : 시작 지점

     * @param pLength : 몇자리 자를 것인지

     * @return

     */

    public static String subStringByLength( String value, int startIdx, int pLength ) {

String result = "";

if( isEmpty( value ) ) return "";


value = value.trim();


int iValueLength = value.length(); //= 문자의 길이를 구함

if( iValueLength < startIdx ) return "";

//= Parameter로 넘어온 길이(pLength)를 넘지 않는 한에서 길이를 제공

int iLength = (iValueLength >= pLength)? pLength : iValueLength;


result = value.substring( startIdx, iLength );


return result;

}




3. 널일경우 0으로 변환 (INT, STRING) 모두..


/**

     * null을 0으로 바꾸기

     * @param str 처리 할 문자열 

     * @return int

     */

    public static int convertStrToInt(String str) {

    if (str == null || "".equals(str)) {

return 0;

}

    try {

    return Integer.parseInt(str);

    } catch(NumberFormatException nfe) {

    return 0;

    }

    }

    

    /**

     * null을 0으로 변경

     * @param num

     * @return

     */

    public static int convertIntegerToInt(Integer num) {

    if (num == null || "".equals(num)) {

return 0;

}

    try {

    return (int)num;

    } catch(NumberFormatException nfe) {

    return 0;

    }

    }

    

    /**

     * null을 0으로 바꾸기

     * @param str 처리 할 문자열 

     * @return String

     */

public static String convertStrToZero(String str){

if (str == null || str.trim().equals("") || str.equals("null")){

return "0";

}

return str.trim();

}




4. INT(숫자) 여부 판단.


/**

 * 숫자인지 여부 확인

 * @param s

 * @return

 */

public static boolean isStringDoubleChk(String s) {

try {

Double.parseDouble(s);

return true;

} catch (NumberFormatException e) {

return false;

}

}




5. 특정 문자열 자르기

/**

  * 문자열에서 특정 문자를 제거하기

  * @param orgStr

  * @param removeStr

  * @return

  */

  public static String removeStr(String orgStr, String removeStr)

  {

  if(orgStr == null)

  return "";

    else

    return orgStr.replaceAll(removeStr, "");

  }



6. BYTE SIZE 가져오기

/**

  *  byte size를 가져온다.

  *

  * @param str String target

  * @return int bytelength

  */

  public static int getByteSize(String str){

   if (str == null || str.length() == 0)

    return 0;

   byte[] byteArray = null;

    try{

    byteArray = str.getBytes("UTF-8");

   }catch (UnsupportedEncodingException ex){}

   if (byteArray == null) return 0;

   return byteArray.length;

  }



7. 앞에 0을 채워넣어 자릿수 맞춤

/**

 * 앞에 0을 채워넣어 자릿수 맞춤

 * @param length

 * @param nValue

 * @return

 * */

public static String setNumLength(int length, String szValue){

String szResult = "";

int nResult = 0;

if(isNumber(szValue))

nResult = Integer.parseInt(szValue);

else

nResult = 0;

szResult = String.format("%0"+length+"d", nResult);

return szResult;

}




반응형