Created
July 7, 2014 18:19
-
-
Save scottmarlow/5ae00fe0af4ae45e0876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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