public static String replace ( final String str , final Map < String , String > params ) { final StringBuilder sb = new StringBuilder ( str . length ( ) ) ; final char [ ] strArray = str . toCharArray ( ) ; int i = 0 ; while ( i < strArray . length - 1 ) { if ( strArray [ i ] == VAR_START && strArray [ i + 1 ] == VAR_LEFT_BOUND ) { i = i + 2 ; final int begin = i ; while ( strArray [ i ] != VAR_RIGHT_BOUND ) { ++ i ; } final String var = str . substring ( begin , i ++ ) ; Preconditions . checkState ( params . containsKey ( var ) , "No value provided for variable '%s' " + "in string '%s'" , var , str ) ; final String value = params . get ( var ) . trim ( ) ; sb . append ( value ) ; } else { sb . append ( strArray [ i ] ) ; ++ i ; } } if ( i < strArray . length ) { sb . append ( strArray [ i ] ) ; } return sb . toString ( ) ; }
Replace placeholders in string .