1

I've got a String like this:

Very long text ... ${ABC} ... very long text ... ${DEF} ...
...
very long text ... ${GHI} ... very long text ...
...
... and so forth ...

and I need to replace all these ${...} with some value (say also such value can be very long) which is stored in a variable of type String.

Until now I'm solving it like follows:

String finalString = templateString
                .replaceAll("\\$\\{ABC}", abc)
                .replaceAll("\\$\\{DEF}", def)
                .replaceAll("\\$\\{GHI}", ghi)
                ...

but I'm sure it is not the best way of doing this.

I can't use String.format() because the template variables are named.

Do you know any way of doing this (maybe using StringBuilder?) without using external libraries?

user402843
  • 145
  • 8
  • 1
    Take a look at this answer if you don't want to use external libs : https://stackoverflow.com/questions/2286648/named-placeholders-in-string-formatting#answer-27815924 – Eritrean Mar 01 '22 at 13:50
  • *but I'm sure it is not the best way of doing this.* – Harald Mar 01 '22 at 15:39

0 Answers0