I have a string

String templateString = "The ${animal} jumps over the ${target}.";
valuesMap.put("animal", "quick brown fox");
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);

But there is no entry for attr target in valuesMap. Final resolvedString would be The quick brown fox jumps over the ${target}.

Instead of ${target}, it need to be empty. Values in templatestring which doesn't have key in map should be empty or null.

required The quick brown fox jumps over the.

How to handle this

1

There are 1 answers

2
dariosicily On

Your Map<String,String> valuesMap contains just the couple key, value "animal", "quick brown fox", you have to add the couple "target", "" to your map like below:

String templateString = "The ${animal} jumps over the ${target}.";
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", ""); //<-- adding the new couple to the map
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);