Skip to content

Instantly share code, notes, and snippets.

@scottmarlow
Created July 7, 2014 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottmarlow/711023119156fe9e5ead to your computer and use it in GitHub Desktop.
Save scottmarlow/711023119156fe9e5ead to your computer and use it in GitHub Desktop.
/**
* Substitute sub-strings in side of a string.
*
* @param stringBuilder String buffer to use for substitution (buffer is not reset)
* @param from String to substitute from
* @param to String to substitute to
*/
private static void subst(final StringBuilder stringBuilder, final String from, final String to) {
int begin = 0, end = 0;
while ((end = stringBuilder.indexOf(from, end)) != -1) {
stringBuilder.delete(end, end + from.length());
stringBuilder.insert(end, to);
// update positions
begin = end + to.length();
end = begin;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment