Skip to content

Instantly share code, notes, and snippets.

@scottmarlow
Created July 8, 2014 12:23
Show Gist options
  • Save scottmarlow/28e88adb84f1351de2f9 to your computer and use it in GitHub Desktop.
Save scottmarlow/28e88adb84f1351de2f9 to your computer and use it in GitHub Desktop.
used for search/replace within a string.
package org.jboss.as.jpa.hibernate4.management;
/**
* StringReplace
*
* @author Scott Marlow
*/
public final class StringReplace {
private final String find;
private final String replacement;
public StringReplace(String find, String replacement) {
this.find = find;
this.replacement = replacement;
}
/**
* Substitute sub-strings inside of a string.
* @param input buffer to apply replacements in
* @param replacements to be made
*
*/
public static String replace(String input, StringReplace[] replacements) {
final StringBuilder stringBuilder = new StringBuilder(input);
for (StringReplace stringReplace : replacements) {
int end = 0;
while ((end = stringBuilder.indexOf(stringReplace.find, end)) != -1) {
stringBuilder.delete(end, end + stringReplace.find.length());
stringBuilder.insert(end, stringReplace.replacement);
// update positions
end = end + stringReplace.replacement.length();
}
}
return stringBuilder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment