public static String replace(String text, String repl, String with, int max) { if (isEmpty(text) || isEmpty(repl) || with == null || max == 0) { return text; } int start = 0; int end = text.indexOf(repl, start); if (end == -1) { return text; } int replLength = repl.length(); int increase = with.length() - replLength; increase = (increase < 0 ? 0 : increase); increase *= (max < 0 ? 16 : (max > 64 ? 64 : max)); StringBuffer buf = new StringBuffer(text.length() + increase); while (end != -1) { buf.append(text.substring(start, end)).append(with); start = end + replLength; if (--max == 0) { break; } end = text.indexOf(repl, start); } buf.append(text.substring(start)); return buf.toString();
public static String replace(String text, String repl, String with) { return replace(text, repl, with, -1);
    public static String replace(String text, String repl, String with, int max) {        if (isEmpty(text) || isEmpty(repl) || with == null || max == 0) {            return text;        }        int start = 0;        int end = text.indexOf(repl, start);        if (end == -1) {            return text;        }        int replLength = repl.length();        int increase = with.length() - replLength;        increase = (increase < 0 ? 0 : increase);        increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));        StringBuffer buf = new StringBuffer(text.length() + increase);        while (end != -1) {            buf.append(text.substring(start, end)).append(with);            start = end + replLength;            if (--max == 0) {                break;            }            end = text.indexOf(repl, start);        }        buf.append(text.substring(start));        return buf.toString();    }