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