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 OverflowBaeldung
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 ...
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
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
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
[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
Videos
r/java on Reddit: What Happened to Java's String Templates? Inside ...
14:44
String Templates - Syntactic sugar or useful improvement? - YouTube
09:18
Java 21 String Templates | Java 21 New Features - YouTube
14:43
What Happened to Java's String Templates? Inside Java Newscast ...
Are You Using This NEW Java Feature? - YouTube
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.
Top answer 1 of 12
198
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);
2 of 12
155
Take a look at the java.text.MessageFormat class, MessageFormat takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.
CopyObject[] params = new Object[]{"hello", "!"};
String msg = MessageFormat.format("{0} world {1}", params);
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.
Reddit
reddit.com › r/java › there will be no string template in jdk 23.
r/java on Reddit: There will be no String Template in JDK 23.
April 6, 2024 -
"there will be no string template feature, even with --enable-preview, in JDK 23" https://mail.openjdk.org/pipermail/amber-spec-experts/2024-April/004106.html
Top answer 1 of 5
144
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 :)
2 of 5
36
TBH I would be totally fine with C# style (dolar-prefix of string and expression): $"{x} plus {y} equals {x + y}" Clearly indicated intention, no issue with conflict with legacy strings...
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.
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
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.