Skip to content

Instantly share code, notes, and snippets.

@suxiaogang
Created May 13, 2013 12:29
Show Gist options
  • Save suxiaogang/5567973 to your computer and use it in GitHub Desktop.
Save suxiaogang/5567973 to your computer and use it in GitHub Desktop.
public static String replaceLast(String string, String toReplace,
String replacement) {
int pos = string.lastIndexOf(toReplace);
if (pos > -1) {
return string.substring(0, pos)
+ replacement
+ string.substring(pos + toReplace.length(),
string.length());
} else {
return string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment