Java library that includes the $ {var} lookup

Does anyone know of a Java library that supports style substitution of $ {var} in text files?

+3
source share
4 answers
+7
source

The Java MessageFormat class allows semisimple text replacement, with support for localizing and formatting numbers, dates, and times. And for pluralization, you can use java.text.ChoiceFormat with MessageFormat. This may be enough for all your text replacement needs.

, Apache (http://velocity.apache.org/) Freemarker (http://freemarker.sourceforge.net/)

+3

Don’t miss the StringTemplate , this is probably the cleanest separation of data and template problems and very reliable.

+2
source

Use org.apache.commons.lang3.text.StrSubstitutor

Example 1 (the simplest example is to use this class to replace Java System properties):

  StrSubstitutor.replaceSystemProperties(
    "You are running with java.version = ${java.version} and os.name = ${os.name}.");

Example 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);

getting:

   The quick brown fox jumped over the lazy dog.
+2
source

Source: https://habr.com/ru/post/1735326/


All Articles