Skip to content

Instantly share code, notes, and snippets.

@scottmarlow
Created July 7, 2014 18:19
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/5ae00fe0af4ae45e0876 to your computer and use it in GitHub Desktop.
Save scottmarlow/5ae00fe0af4ae45e0876 to your computer and use it in GitHub Desktop.
/**
* Substitute sub-strings in side of a string.
*
* @param buff Stirng buffer to use for substitution (buffer is not reset)
* @param from String to substitute from
* @param to String to substitute to
* @param string String to look for from in
* @return Substituted string
*/
public static String subst(final StringBuffer buff, final String from, final String to, final String string)
{
int begin = 0, end = 0;
while ((end = string.indexOf(from, end)) != -1)
{
// append the first part of the string
buff.append(string.substring(begin, end));
// append the replaced string
buff.append(to);
// update positions
begin = end + from.length();
end = begin;
}
// append the rest of the string
buff.append(string.substring(begin, string.length()));
return buff.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment