/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.munjaon.client.util;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * 문자열 관련 유틸리티 클래스
 * @author JDS
 */
public final class StringUtil {

    public static final String SystemEncoding = getSystemEncoding();

    public static String getSystemEncoding() {
        return (new java.io.OutputStreamWriter(System.out)).getEncoding();
    }

    public static String trim(byte[] obj) {
        if( obj == null || obj.length == 0 ) {
            return "";
        }

        return (new String(obj)).trim();
    }

    public static String trim(Object obj) {
        return trim(obj, "");
    }

    public static String trim(Object obj, String dflt) {
        if( obj == null ) {
            return dflt;
        }

        return ((String)obj).trim();
    }

    public static String ltrim(byte[] obj) {
        return ltrim(new String(obj));
    }

    public static String ltrim(String obj) {
        return ltrim(obj.toCharArray());
    }

    public static String ltrim(char[] obj) {
        int len = obj.length;
        int idx = 0;

        while( idx < len && obj[idx] <= ' ' ) {
            idx++;
        }

        return new String(obj, idx, len-idx);
    }

    public static String rtrim(byte[] obj) {
        return rtrim(new String(obj));
    }

    public static String rtrim(String obj) {
        return rtrim(obj.toCharArray());
    }

    public static String rtrim(char[] obj) {
        int len = obj.length;
        int idx = len-1;

        while( idx >= 0 && obj[idx] <= ' ' ) {
            idx--;
        }

        return new String(obj, 0, idx+1);
    }

    public static String replaceAll(String src, String from, String to) {
        StringBuilder sbuf = new StringBuilder();

        int len = from.length();
        int idx = 0;
        int stx = 0;

        while( (idx=src.indexOf(from, stx)) > -1 ) {
            sbuf.append(src.substring(stx, idx));
            sbuf.append(to);
            stx=idx+len;
        }

        sbuf.append(src.substring(stx));

        return sbuf.toString();
    }

    public static String[] split(String sSrc, String sDelim) {
        ArrayList aList = new ArrayList();

        String sTmp;
        int len = sDelim.length();
        int idx = 0;
        int stx = 0;

        while( (idx=sSrc.indexOf(sDelim, stx)) > -1 ) {
            sTmp = sSrc.substring(stx, idx);
            aList.add(sTmp);
            stx=idx+len;
        }

        if( stx <= sSrc.length() ) {
            aList.add(sSrc.substring(stx));
        }

        String[] sRet = new String[aList.size()];

        for( int i=0; i<aList.size(); i++ ) {
            sRet[i] = (String) aList.get(i);
        }

        return sRet;
    }

    public static String substring(String obj, int idx, int length) {
        if( obj.getBytes().length <= idx ) {
            return "";
        }

        if( obj.getBytes().length <= length ) {
            return obj;
        }

        int totallen=0;
        int i=idx;
        for( i=idx; i<obj.length(); i++ ) {
            totallen += obj.substring(i, i+1).getBytes().length;

            if( length < totallen ) {
                break;
            }
        }

        return obj.substring(idx, i);
    }

    public static String substring(String obj, int length) {
        return substring(obj, 0, length);
    }

    public static String numFilter(String src) {
        return src.replaceAll("[^0-9]", "");
    }

    public static String toNumberFormat(Object obj, String fmt) {
        DecimalFormat df = new DecimalFormat(fmt);
        return df.format(obj);
    }

    public static String pad0(String str, int size) {
        char[] zeros = new char[size - str.length()];
        for (int i = 0; i < zeros.length; i++)
            zeros[i] = '0';
        return new String(zeros) + str;
    }
}
