允许$​​ {var}替换的Java库

Dou*_*sek 5 java text

有谁知道一个Java库,它为文本文件中的$ {var}样式替换提供支持?

use*_*334 8

使用 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)