public static String substitute ( String input , Map < String , Object > values ) { StringBuilder output = new StringBuilder ( input . length ( ) ) ; int index = 0 ; Matcher matcher = SUBST_PATTERN . matcher ( input ) ; while ( matcher . find ( ) ) { String match = matcher . group ( ) ; output . append ( input . substring ( index , matcher . start ( ) ) ) ; Object value = values . get ( match . substring ( 2 , match . length ( ) - 2 ) ) ; output . append ( value == null ? "" : String . valueOf ( value ) ) ; index = matcher . end ( ) ; } output . append ( input . substring ( index ) ) ; return output . toString ( ) ; }
Simple substitution mechanism . Missing values substituted with empty string .