public static String replace(String s,String sub,String with){
  int c=0;
  int i=s.indexOf(sub,c);
  if (i == -1) {
    return s;
  }
  int length=s.length();
  StringBuilder sb=new StringBuilder(length + with.length());
  do {
    sb.append(s.substring(c,i));
    sb.append(with);
    c=i + sub.length();
  }
 while ((i=s.indexOf(sub,c)) != -1);
  if (c < length) {
    sb.append(s.substring(c,length));
  }
  return sb.toString();
}

public static String replace ( String s , String sub , String with ) { int c = 0 ; int i = s . indexOf ( sub , c ) ; if ( i == - 1 ) { return s ; } int length = s . length ( ) ; StringBuilder sb = new StringBuilder ( length + with . length ( ) ) ; do { sb . append ( s . substring ( c , i ) ) ; sb . append ( with ) ; c = i + sub . length ( ) ; } while ( ( i = s . indexOf ( sub , c ) ) != - 1 ) ; if ( c < length ) { sb . append ( s . substring ( c , length ) ) ; } return sb . toString ( ) ; }
replaces all occurrences of a certain pattern in a string with a replacement string . this is the fastest replace function known to author .
