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 - Also, if the processor fails to interpolate, it can generate an Exception. Java decides to treat “<some text>” as a StringLiteral or StringTemplate based on the presence of the embedded expressions.
🌐
Reddit
reddit.com › r/javahelp › template literals in java
r/javahelp on Reddit: Template literals in Java
December 30, 2020 -

I know in JavaScript there are template strings that allow you to do this:

const firstStr = 'hello';
const secondStr = 'world';
const templateStr = `${firstStr} ${secondStr}`;

I can't seem to find anything that lets you do this in Java. Is there the same capability in Java? Concatenation just looks ugly to me. I saw this:

https://teamtreehouse.com/community/are-there-no-template-literals-in-java

but it requires your output to be put through

System.out.printf();

instead of

System.out.printl();

I'm specifically looking for a way to store a template literal in a variable like line 3 in the example above.

Thanks!

🌐
Oracle
docs.oracle.com › en › java › javase › 21 › language › string-templates.html
Java Language Updates
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 text in the string template.
🌐
OpenJDK
openjdk.org › jeps › 465
JEP 465: String Templates (Third Preview)
January 9, 2024 - WHERE ..., an HTML document follows <html> ... </html>, and even a message in a natural language follows a template that intersperses dynamic values (e.g., a username) amongst literal text. Each kind of template has rules for validation and transformation, such as "escape all quotes" for SQL statements, "allow only legal character entities" for HTML documents, and "localize to the language configured in the OS" for natural-language messages. Ideally a string's template could be expressed directly in the code, as if annotating the string, and the Java runtime would apply template-specific rules to the string automatically.
🌐
OpenJDK
openjdk.org › jeps › 430
JEP 430: String Templates (Preview)
September 17, 2021 - WHERE ..., an HTML document follows <html> ... </html>, and even a message in a natural language follows a template that intersperses dynamic values (e.g., a username) amongst literal text. Each kind of template has rules for validation and transformation, such as "escape all quotes" for SQL statements, "allow only legal character entities" for HTML documents, and "localize to the language configured in the OS" for natural-language messages. Ideally a string's template could be expressed directly in the code, as if annotating the string, and the Java runtime would apply template-specific rules to the string automatically.
🌐
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 - String Templates (a preview feature ... pretty much says it all – String Templates. Think of it as patterns that include String literals and variable values....
Find elsewhere
🌐
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.
🌐
Team Treehouse
teamtreehouse.com › community › are-there-no-template-literals-in-java
Are there no template literals in java? (Example) | Treehouse Community
February 21, 2018 - String concatenation is kinda ugly and boring. JavaScript got template literals and interpolations recently, I was wondering if there is something equivalent to that in Java.
🌐
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 - The new way to work with Strings in Java is called template expression, a programmable way of safely interpolating expressions in String literals.
🌐
OpenJDK
openjdk.org › jeps › 459
JEP 459: String Templates (Second Preview)
August 14, 2023 - WHERE ..., an HTML document follows <html> ... </html>, and even a message in a natural language follows a template that intersperses dynamic values (e.g., a username) amongst literal text. Each kind of template has rules for validation and transformation, such as "escape all quotes" for SQL statements, "allow only legal character entities" for HTML documents, and "localize to the language configured in the OS" for natural-language messages. Ideally a string's template could be expressed directly in the code, as if annotating the string, and the Java runtime would apply template-specific rules to the string automatically.
🌐
How to do in Java
howtodoinjava.com › home › java 21 string templates
Java 21 String Templates (with Examples)
July 30, 2024 - The only difference is that Java ... SQL statements, XML nodes etc. Syntactically, a template expression resembles a string literal with a prefix denoting the template processor....
🌐
Medium
medium.com › @viraj_63415 › java-21-string-templates-79fd908f30ff
Java String Templates — A Better Interpolation | by Viraj Shetty | Medium
June 19, 2024 - I also have a discounted course (12.99$ or best local price) on Udemy called Java Virtual Threads and Structured Concurrency which takes a comprehensive look at Virtual Threads, Structured Concurrency, Scoped Values and their integration with Spring Boot · In summary, String Templates provide us with several benefits including · Making it easy to express strings that include values computed at run time · Enabling the creation of non-string values computed from literal text
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › › java.base › java › lang › StringTemplate.html
StringTemplate (Java SE 22 & JDK 22)
July 16, 2024 - 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.
🌐
Hacker News
news.ycombinator.com › item
JEP draft: String Templates (Final) | Hacker News
January 15, 2024 - f"" vs STR."" is really a quite minimal difference, and I think worth it. Having the "." operator here mean "interpolate RHS using LHS" seems a nice balance between terseness and explicitness · As for "\{" being a divergent choice, frankly who cares
🌐
NxtWave
ccbp.in › blog › articles › literals-in-java
Literals in Java: Types & Examples
January 31, 2025 - An integer literal in Java should ... = 0b123; // Error: Invalid binary number ... The Java template literals aren't directly supported in the way they are in JavaScript....
🌐
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 - Exactly that’s what is done by the template expression that Java adheres to as opposed to String interpolation used by other popular programming languages. The design of the template expression makes it impossible to go directly from a string literal or text block with embedded expressions to a string with the expressions' values interpolated.
🌐
Medium
medium.com › @kaustubh.saha › string-templates-in-java-21-and-22-46413472c2b3
String Templates in Java 21 and 22 | by Kaustubh Saha | Medium
December 4, 2025 - Here is a deep dive into Java String Templates and why they are more than just syntactic sugar. A String Template couples literal text with embedded expressions, processed at runtime.
🌐
Medium
gdams.medium.com › harnessing-the-power-of-string-templates-a-guide-for-java-developers-8b88a56fb452
Harnessing the Power of String Templates: A Guide for Java Developers | by George Adams | Medium
April 14, 2023 - String templates, set to be introduced as a preview feature in JDK 21, simplify string formatting and manipulation in Java. This feature allows developers to embed expressions directly within string literals, making it easier to build and format ...
🌐
nipafx
nipafx.dev › inside-java-newscast-71
What Happened to Java's String Templates? Inside Java Newscast #71 // nipafx
JDK 23 doesn't contain the string template preview that was present in JDKs 21 and 22. Let's discuss why the feature was removed (probably not for the reasons you think), what a new proposal could look like, and when we may see it.
Published   June 20, 2024