使用 org.apache.commons.lang3.text.StrSubstitutor
示例1(最简单的示例是使用此类替换Java System属性):
StrSubstitutor.replaceSystemProperties(
"You are running with java.version = ${java.version} and os.name = ${os.name}.");
Run Code Online (Sandbox Code Playgroud)
范例2:
Map valuesMap = HashMap();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumped over the ${target}.";
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
Run Code Online (Sandbox Code Playgroud)
产生:
The quick brown fox jumped over the lazy dog.
Run Code Online (Sandbox Code Playgroud)