Use StringSubstitutor from Apache Commons Text.

Dependency import

Import the Apache commons text dependency using maven as bellow:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.10.0</version>
</dependency>

Example

Map<String, String> valuesMap = new HashMap<String, String>();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumped over the ${target}.";
StringSubstitutor sub = new StringSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
Answer from JH. on Stack Overflow
๐ŸŒ
Oracle
docs.oracle.com โ€บ en โ€บ java โ€บ javase โ€บ 21 โ€บ language โ€บ string-templates.html
String Templates
January 16, 2025 - String templates complement Java's existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results. An embedded expression is a Java expression except it has additional syntax to differentiate it from the literal ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ string templates in java
String Templates in Java | Baeldung
July 7, 2025 - The same is followed for โ€œโ€โ€<some text>โ€โ€โ€ to distinguish between TextBlock and TextBlockTemplate. This distinction is important to Java because, even though in both cases itโ€™s wrapped between double quotes(โ€œโ€), a String template is of type java.lang.StringTemplate, an interface, and not the java.lang.String.
Discussions

String replacement in java, similar to a velocity template - Stack Overflow
Java 23 withdrew/removed the feature. See the string templates proposal (JEP 430) here. More on stackoverflow.com
๐ŸŒ stackoverflow.com
What Happened to Java's String Templates? Inside Java Newscast
Honestly, this whole thing with string templates in java feels like a paranoia. Security? Validation? The hell are they smokin there? Why are they trying to solve world hunger with it? Just give people the damn interpolation like all normal human beings have other languages that's all we want. More on reddit.com
๐ŸŒ r/java
122
66
June 20, 2024
There will be no String Template in JDK 23.
wow. It's interesting that Brian Goetz recently said he was ready to finalize the feature and move on, and all the complaints were nothing they hadn't anticipated and discussed extensively. They had the JEP submitted to finalize this for Java 22 and they held off to a second unchanged preview, and then were going to finalize for Java 23, and now, even Goetz is ready to move the whole thing back to a complete redesign. As a Java dev I'd rather get this later with a better design than earlier with a less perfect design. It is interesting how close the existing design was to being finalized. The preview system works :) More on reddit.com
๐ŸŒ r/java
131
122
April 6, 2024
Java string interpolation feature has been cancelled
Brian Goetz is one of the Java language&JDK architects ยท https://mail.openjdk.org/pipermail/amber-spec-experts/2024-A More on news.ycombinator.com
๐ŸŒ news.ycombinator.com
114
71
June 25, 2024
๐ŸŒ
OpenJDK
openjdk.org โ€บ jeps โ€บ 459
JEP 459: String Templates (Second Preview)
Enhance the Java programming language with string templates. String templates complement Java's existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results.
๐ŸŒ
nipafx
nipafx.dev โ€บ inside-java-newscast-71
What Happened to Java's String Templates? Inside Java Newscast #71 // nipafx
This triggered a super-interesting discussion but it's length and breadth indicated that this issue wouldn't be solved in the remaining 12 weeks or so before JDK 23 would be branched and so the decision was made to remove string templates entirely. And as we've touched on in the last Inside Java Newscast, that's exactly what happened: JDK 23 contains no string templates at all and once you update your experimental and hobby code bases, you'll have to rip out everything related to string templating, which isn't particularly fun.
Published: June 20, 2024
๐ŸŒ
JetBrains
blog.jetbrains.com โ€บ home โ€บ intellij idea โ€บ string templates in java โ€“ why should you care?
String Templates in Java - why should you care? - The JetBrains Blog
November 27, 2023 - TLDR; The existing String concatenation ... (a preview feature introduced in Java 21) greatly improves how we create strings in Java by merging constant strings with variable values....
๐ŸŒ
Stringtemplate
stringtemplate.org
StringTemplate
StringTemplate is a java template engine (with ports for C#, Objective-C, JavaScript, Scala) for generating source code, web pages, emails, or any other formatted text output. StringTemplate is particularly good at code generators, multiple site skins, and internationalization / localization.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @ucgorai โ€บ string-templates-in-java-21-simplifying-string-construction-9ba9866ff510
String Templates in Java 21: Simplifying String Construction | by Uma Charan Gorai | Medium
June 19, 2025 - Since templates are parsed at compile time, the JVM can generate optimized bytecode. Reduces runtime parsing and boosts performance. Fewer intermediate objects means less GC pressure, improving throughput. Internally uses StringBuilder-like efficient append mechanisms without developer overhead. ... import java.util.function.Function; import java.util.template.*; import static java.util.template.TemplateProcessors.STR; public class StringTemplateExamples { public static void main(String[] args) { basicExample(); oldWayComparison(); customProcessorExample(); multiLineExample(); performanceComparison(); } // 1.
๐ŸŒ
Medium
medium.com โ€บ @viraj_63415 โ€บ java-21-string-templates-79fd908f30ff
Java String Templates โ€” A Better Interpolation | by Viraj Shetty | Medium
June 19, 2024 - With Java 21, Oracle has introduced a Preview feature called String Templates which brings interpolation to Java and much more.
๐ŸŒ
Belief Driven Design
belief-driven-design.com โ€บ looking-at-java-21-string-templates-c7cbc
Looking at Java 21: String Templates | belief driven design
June 20, 2023 - Even though you save on the number of String allocations, now the JVM has to parse/validate the template String. The java.text.MessageFormat type is like the older sibling of String::format, as it uses the same approach of using format String container specifiers.
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ what happened to java's string templates? inside java newscast
r/java on Reddit: What Happened to Java's String Templates? Inside Java Newscast
June 20, 2024 - But that's literally what string interpolation is in other languages: just a handy way of concatenating strings. Why is this such a big deal for java? You can't just make secure templates and have magically all injection issues solved. Dumb people will find a way.
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ java โ€บ string-templates
String Templates in Java
June 12, 2025 - STR is the name of a so-called template processor (more precisely: a constant of type StringTemplate.Processor automatically imported into every Java file). A template processor defines how template text and placeholders are combined.
๐ŸŒ
JVM Advent
javaadvent.com โ€บ home โ€บ beyond the basics: elevating java string handling with custom template processors
Beyond the Basics: Elevating Java String Handling with Custom Template Processors - JVM Advent
December 23, 2023 - At the core of String Templates is a new syntax for Java called โ€˜template expressionsโ€™. This syntax enables efficient and secure string interpolation, going beyond simple string concatenation to transform structured text into various object types based on predefined rules.
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
Java string interpolation feature has been cancelled | Hacker News
June 25, 2024 - Brian Goetz is one of the Java language&JDK architects ยท https://mail.openjdk.org/pipermail/amber-spec-experts/2024-A
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java
Guide to String Templates in Java 21 - Java Code Geeks
March 6, 2024 - Each of the methods mentioned has its effectiveness, yet they come with limitations. Either they make the code hard to understand or make it too long for simple tasks. Java 21 introduced a preview feature called string templates, bringing a much-anticipated addition to the string formatting landscape.
๐ŸŒ
At15
at15.dev โ€บ string templates
Java 21 String Templates | at15 blog
import static java.lang.StringTemplate.STR; class Main { public static void main(String[] args) { var name = "World"; System.out.println(STR."Hello, \{name}!"); System.out.println(STR.""" Hello!
๐ŸŒ
JavaDZone
javadzone.com โ€บ java-string-templates-practical-examples
Java String Templates in Java 21: Practical Examples - JavaDZone
March 27, 2024 - Template strings, often known as ... and Angular interpolation. Essentially, they allow us to insert variables into a string, and the variable values are determined at runtime....
๐ŸŒ
AlgoMaster
algomaster.io โ€บ home โ€บ java โ€บ string templates (java 21)
String Templates (Java 21) | Java | AlgoMaster.io
June 6, 2026 - The proposed answer was to combine a template (the string with embedded expressions) with a processor (a small class that decides what the template means). This lesson walks through the model, the three built-in processors, and the main feature, custom processors that escape SQL and HTML automatically.