0

code:

Map<String, Number> valueMap = new HashMap<>();
valueMap.put("amount", 20.0);
valueMap.put("id", 123456);
StringSubstitutor ss = new StringSubstitutor(valueMap);
// I expect output: $123456===$20.0
System.out.println(ss.replace("${id}===${amount}"));  // output: 123456===20.0
System.out.println(ss.replace("$${id}===$${amount}"));// output: ${id}===${amount}
System.out.println(ss.replace("$$${id}===$$${amount}")); // output: $${id}===$${amount}

I expect output: $123456===$20.0, how to do?

Guo
  • 1,761
  • 2
  • 22
  • 45

1 Answers1

1

You can achieve it by setting an escape char as shown in below example. I have tested it on my local machine and its working as expected:

Map<String, Number> valueMap = new HashMap<>();
valueMap.put("amount", 20.0);
valueMap.put("id", 123456);

StringSubstitutor ss = new StringSubstitutor(valueMap);
ss.setEscapeChar('\\');

System.out.println(ss.replace("$${id}===$${amount}"));  // output: $123456===$20.0

Please refer java docs

pcsutar
  • 1,715
  • 2
  • 9
  • 14
  • `setEscapeChar('\\');` is working as the [default escape character is '$'](https://commons.apache.org/proper/commons-text/apidocs/src-html/org/apache/commons/text/StringSubstitutor.html#line.248) – samabcde Jun 01 '21 at 14:27