public static String applyReplacements ( CharSequence word , LinkedHashMap < String , String > replacements ) { // quite horrible from performance point of view; this should really be a transducer. StringBuilder sb = new StringBuilder ( word ) ; for ( final Map . Entry < String , String > e : replacements . entrySet ( ) ) { String key = e . getKey ( ) ; int index = sb . indexOf ( e . getKey ( ) ) ; while ( index != - 1 ) { sb . replace ( index , index + key . length ( ) , e . getValue ( ) ) ; index = sb . indexOf ( key , index + key . length ( ) ) ; } } return sb . toString ( ) ; }
Apply partial string replacements from a given map .