Use StringSubstitutor from Apache Commons Text.

Dependency import

Import the Apache commons text dependency using maven as bellow:

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

Example

CopyMap<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
🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › language › string-templates.html
4 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 ...
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
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
String.format() is 3x faster in Java 17
Glad to see some of the small enhancements we did in 17 get recognition. Not sure if this one matters, but String::format shows up in profiles every now and then so it felt reasonable to me to give it some TLC in between larger projects. More on reddit.com
🌐 r/java
41
298
October 30, 2021
[Java] Using String.format to format a string printed out of my own toString method, I am not able to right align the text though.
Format specifiers can contain an optional width field. For example, %20s will set the minimum width of the String to 20 characters (right-aligned). You can find the documentation here for more info. More on reddit.com
🌐 r/learnprogramming
2
9
March 15, 2014
🌐
OpenJDK
openjdk.org › jeps › 465
JEP 465: String Templates (Third Preview)
January 9, 2024 - String Templates were originally proposed as a preview feature by JEP 430, delivered in JDK 21, and previewed a second time by JEP 459, delivered in JDK 22. We here propose to finalize the feature with no further changes. Simplify the writing of Java programs by making it easy to express strings that include values computed at run time.
🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › StringTemplate.html
StringTemplate (Java SE 21 & JDK 21)
January 20, 2026 - In the source code of a Java program, a string template or text block template contains an interleaved succession of fragment literals and embedded expressions. The fragments() method returns the fragment literals, and the values() method returns the results of evaluating the embedded expressions.
🌐
JetBrains
blog.jetbrains.com › idea › 2023 › 11 › string-templates-in-java-why-should-you-care
String Templates in Java - why should you care? | The IntelliJ IDEA 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....
Find elsewhere
🌐
OpenJDK
openjdk.org › jeps › 430
JEP 430: String Templates (Preview)
September 17, 2021 - 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.
🌐
How to do in Java
howtodoinjava.com › home › java 21 string templates
Java 21 String Templates (with Examples)
July 30, 2024 - Since Java 21, we can create string templates containing the embedded expressions (evaluated at runtime).
🌐
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
🌐
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 - Whenever we have a String-based template that requires transformation, validation, or sanitizing, Java’s String templates will give us a built-in simplistic template engine without requiring a third-party dependency.
🌐
OpenJDK
openjdk.org › jeps › 459
JEP 459: String Templates (Second Preview)
August 14, 2023 - 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.
🌐
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.
🌐
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....
🌐
Medium
maffonso.medium.com › java-string-templates-simplifying-text-handling-1f36f864056e
Java String Templates: Simplifying Text Handling | by Mauricio Afonso | Medium
June 14, 2024 - To understand String Templates, we must start by talking about the structure of this feature. Template Processor + Template + Template Expressions ... mechanism that will process the text STR is a template processor defined in the Java Platform.
🌐
InfoQ
infoq.com › news › 2023 › 04 › java-gets-a-boost-with-string
Java Gets a Boost with String Templates: Simplifying Code and Improving Security - InfoQ
April 28, 2023 - JEP 430, String Templates (Preview), ... with string templates, which are similar to string literals but contain embedded expressions incorporated into the string template at run time....