@ReplaceSubString in Java


Java


String result = replaceSubString("Johan Känngård","Johan", "Nisse");

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;
}

Posted by fbrefere001 on Sunday April 5, 2009