public static String replaceStringInString ( final String text , final String searchString , final String replacement ) { if ( stringIsBlank ( text ) || stringIsBlank ( searchString ) || replacement == null ) { return text ; } int start = 0 ; int end = text . indexOf ( searchString , start ) ; if ( end == INDEX_NOT_FOUND ) { return text ; } final int replacedLength = searchString . length ( ) ; final StringBuilder builder = new StringBuilder ( ) ; while ( end != INDEX_NOT_FOUND ) { builder . append ( text . substring ( start , end ) ) . append ( replacement ) ; start = end + replacedLength ; end = text . indexOf ( searchString , start ) ; } builder . append ( text . substring ( start ) ) ; return builder . toString ( ) ; }
Replaces all occurrences of a string within a larger string with a replacement string .