* The returned string will shrink or grow as necessary, depending on * the lengths of v1 and v2. *
* ** Implementation note: This method avoids using the standard String * manipulation methods to increase execution speed. * Using the replace method does however * include two new operations in the case when matches are found. *
* * * @param s Source string. * @param v1 String to be replaced withv2.
* @param v2 String replacing v1.
* @return Modified string. If any of the input strings are null,
* the source string s will be returned unmodified.
* If v1.length == 0, v1.equals(v2) or
* no occurances of v1 is found, also
* return s unmodified.
*/
public static String replace(final String s,
final String v1,
final String v2) {
// return quick when nothing to do
if(s == null
|| v1 == null
|| v2 == null
|| v1.length() == 0
|| v1.equals(v2)) {
return s;
}
int ix = 0;
int v1Len = v1.length();
int n = 0;
// count number of occurances to be able to correctly size
// the resulting output char array
while(-1 != (ix = s.indexOf(v1, ix))) {
n++;
ix += v1Len;
}
// No occurances at all, just return source string
if(n == 0) {
return s;
}
// Set up an output char array of correct size
int start = 0;
int v2Len = v2.length();
char[] r = new char[s.length() + n * (v2Len - v1Len)];
int rPos = 0;
// for each occurance, copy v2 where v1 used to be
while(-1 != (ix = s.indexOf(v1, start))) {
while(start < ix) r[rPos++] = s.charAt(start++);
for(int j = 0; j < v2Len; j++) {
r[rPos++] = v2.charAt(j);
}
start += v1Len;
}
// ...and add all remaining chars
ix = s.length();
while(start < ix) r[rPos++] = s.charAt(start++);
// ..ouch. this hurts.
return new String(r);
}
}