Replacing Substrings in a String
public class Main {
public static void main(String[] argv) throws Exception {
System.out.println(replace("this is a test", "is", "are"));
}
static String replace(String str, String pattern, String replace) {
int start = 0;
int index = 0;
StringBuffer result = new StringBuffer();
while ((index = str.indexOf(pattern, start)) >= 0) {
result.append(str.substring(start, index));
result.append(replace);
start = index + pattern.length();
}
result.append(str.substring(start));
return result.toString();
}
}
Related examples in the same category