0

For example I have:

String templateString = "Hi {{customerName}}, you have successfully ordered a {{itemName}}."
Map<String, String> parameters = new HashMap<>();
parameters.put("customerName", "Bob");
parameters.put("itemName", "sofa");

Desired output: "Hi Bob, you have successfully ordered a sofa."

What would be the best (foolproof, maintainable, time efficient, etc.) way to get the desired output?

I thought of doing something simple:

String output = templateString;
for (Map.Entry<String, String> entry : parameters.entrySet()) {
    output = output.replace("{{" + entry.getKey() + "}}", entry.getValue());
}

Is there a better way?

onepiece
  • 3,279
  • 8
  • 44
  • 63
  • *What would be the best (foolproof, maintainable, time efficient, etc.) way to get the desired output?* - [To not reinvent the wheel.](https://stackoverflow.com/questions/2286648/named-placeholders-in-string-formatting) – BackSlash Sep 17 '18 at 06:56
  • 1
    Nothing wrong at all with your current attempt. What are you looking for here? Note that there are already Java template libraries which can do this. – Tim Biegeleisen Sep 17 '18 at 06:56
  • And there is also the built-in solution using [`printf(...)`](https://docs.oracle.com/javase/10/docs/api/java/io/PrintStream.html#printf(java.lang.String,java.lang.Object...)) – Turing85 Sep 17 '18 at 06:58
  • How about `"Hi " + var1 + " Bye " + var2 + " !! "` – Nicholas K Sep 17 '18 at 06:58
  • You can use built in `String.format()`. – Maciej Mościcki Sep 17 '18 at 06:58
  • @Turing85 It doesn't support named parameters, which is what the OP is asking for I guess – BackSlash Sep 17 '18 at 06:59
  • to all: First, study the task. Before decrementing of the question. – Valentyn Hruzytskyi Sep 17 '18 at 07:02
  • Tim is correct: [Freemarker](https://freemarker.apache.org/) for example does exactly this. Lots of very large companies have used it successfully. – Bohemian Sep 17 '18 at 07:04

4 Answers4

1

Depends how sophisticated a templating system you need. There are many out there already.

Two examples are:

Stewart
  • 17,616
  • 8
  • 52
  • 80
0

Another way is to use Mustache.java, docs

String templateString = "Hi {{customerName}}, you have successfully ordered a {{itemName}}.";
Map<String, String> parameters = new HashMap<>();
parameters.put("customerName", "Bob");
parameters.put("itemName", "sofa");

Writer writer = new StringWriter();
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(new StringReader(templateString), "example");
mustache.execute(writer, parameters);
writer.flush();
System.out.println(writer.toString());
benjamin c
  • 2,278
  • 15
  • 25
0

Would be better to fetch values using keys in Map

            String output = templateString;
            output = output.replace("{{customerName}}",parameters.get("customerName"));
            output = output.replace("{{itemName}}",parameters.get("itemName"));
  • OP is already doing this. The only difference is that he is avoiding hard-coding parameters names instead. – BackSlash Sep 17 '18 at 07:29
0

Apart from the solutions already provided in other answers, you can also use StringSubstitutor from Apache Commons Text.

An example from https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringSubstitutor.html :-

 Map valuesMap = HashMap();
 valuesMap.put("animal", "quick brown fox");
 valuesMap.put("target", "lazy dog");
 String templateString = "The ${animal} jumps over the ${target}.";
 StringSubstitutor sub = new StringSubstitutor(valuesMap);
 String resolvedString = sub.replace(templateString);
Kartik
  • 7,677
  • 4
  • 28
  • 50