1
String[] greetings = {"Hello1", "Hello2", "Hello3"};  
String otherString = "green";
Map<String, String[]> valuesMap = new HashMap<String, String[]>();
valuesMap.put("greeting", greetings);
valuesMap.put("color", otherString );

String templateString = "Its ${color} around. ${greeting} StrSubstitutor APIs.";
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);

I am looking for output similar to:

Its green around. Hello1 StrSubstitutor APIs.
or Hello2 StrSubstitutor APIs
or Hello3 StrSubstitutor APIs.

Any suggestion would be helpful.

tryingSpark
  • 143
  • 2
  • 15
  • Just iterate over your array? – Waize Nov 24 '17 at 12:45
  • Thanks for your response. Edited with more input. Sorry for inconvenience – tryingSpark Nov 24 '17 at 12:49
  • But your variable part is the array of greetings. How should the StrSubstitutor know how much Strings he has to replace/create. You have to iterate over the String[] and execute for each element the Substituor – Waize Nov 24 '17 at 12:52
  • Agree, but is there a way to tell StrSubstitutor to iterate for array variable? – tryingSpark Nov 27 '17 at 06:27
  • I guess you didn't get how the `StrSubstitutor` works. You gave it a `Map` and a `String`, it parses the `String` and if it finds any match with a key from the `Map` it will be replaced by the value of this key. So it's up to you to give the right params. In your example the `StrSubstituor` will always replace greetings with a String representation of your array. Pls read the docs for more info: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/StrSubstitutor.html – Waize Nov 27 '17 at 06:37
  • Yes I understand this. I guess I need to iterate over greetings[] array and keep on appending to a TEMP string and finally substitute using StrSubstitutor. – tryingSpark Nov 27 '17 at 07:30
  • This sounds right. Please provide your working code in the end. – Waize Nov 27 '17 at 09:21
  • How are you putting a String object in `Map` ? I dont think `valuesMap.put("color", otherString );` will compile – ajc Nov 28 '17 at 20:45

0 Answers0