Substitutes variables within a string by values.
This class takes a piece of text and substitutes all the variables within it. The default definition of a variable is${variableName}
. The prefix and suffix can be changed via constructors and set methods.
// Define variables in a map
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("fruit", "apple");
valueMap.put("color", "green");
// template string + variables = resolved string
String replaced = StrSubstitutor.replace("The ${fruit} is ${color} and ${shape:-round}.", valueMap);
// The apple is green and round.
:-
)${
and }
)