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 OverflowOracle
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.
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
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
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
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
Are You Using This NEW Java Feature? - YouTube
14:44
String Templates - Syntactic sugar or useful improvement? - YouTube
Java 21 String Templates (Preview) | STR, FMT & Real-World ...
09:18
Java 21 String Templates | Java 21 New Features
00:57
Open Questions About #Java's String Templates #IJN
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
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.
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.
Top answer 1 of 12
198
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);
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.
Object[] params = new Object[]{"hello", "!"};
String msg = MessageFormat.format("{0} world {1}", params);
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...
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.
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.
Junit
docs.junit.org โบ 6.0.3 โบ overview.html
Overview :: JUnit User Guide
JUnit requires Java 17 (or higher) at runtime.