public static String replace ( String pSource , String pPattern , String pReplace ) { if ( pPattern . length ( ) == 0 ) { return pSource ; // Special case: No pattern to replace } int match ; int offset = 0 ; StringBuilder result = new StringBuilder ( ) ; // Loop string, until last occurence of pattern, and replace while ( ( match = pSource . indexOf ( pPattern , offset ) ) != - 1 ) { // Append everything until pattern result . append ( pSource . substring ( offset , match ) ) ; // Append the replace string result . append ( pReplace ) ; offset = match + pPattern . length ( ) ; } // Append rest of string and return result . append ( pSource . substring ( offset ) ) ; return result . toString ( ) ; }
Replaces a substring of a string with another string . All matches are replaced .