본문 바로가기

Java/[Basic Source]

문자열 치환 - 재귀호출

     /**
     * 재귀 호출로 된 replace
     * @param str : 원본 문자열
     * @param old : 바꿀문자
     * @param new : 바뀔문자
     * @return
     */
    public static String replace(String str, String old, String new)
    {
     int pos = str.indexOf(old);
     if(pos==-1) return str;

     return str.substring(0, pos) + new + replace(str.substring(pos+old.length()),old, new);
    }