/**
* Replaces a subset of a string with another string.
* This function is case sensitive.
*
* Example:
* <code><pre>
* String result = replaceSubString("Johan Känngård",
* "Johan", "Nisse");
* System.out.println(result);
* </pre></code>
* prints the string "Nisse Känngård" to the console
*
* @author Johan Känngård, http://dev.kanngard.net
* @param s - The source string
* @param search - The substring to search for in 's'
* @param replace - The string that will replace 'search'
* @return The changed string
*/
public static String replaceSubString(
String s,
String search,
String replace) {
int p = 0;
while (p < s.length() && (p = s.indexOf(search, p)) >= 0) {
s = s.substring(0, p) + replace +
s.substring(p + search.length(), s.length());
p += replace.length();
}
return s;
}