class="java" name="code">public String replaceLast(String str, String regex, String replacement){ String headStr = str.substring(0, str.lastIndexOf(regex)); String tailStr = str.substring(str.lastIndexOf(regex) + regex.length(), str.length()); return new StringBuilder(headStr).append(replacement).append(tailStr).toString(); }