The string template was a preview feature in Java 21 and Java 22, and has been withdrawn for further redesign. In other words, this feature never existed in mainline Java (you needed to enable preview features to use it), and now with the release of Java 23, it doesn't exist at all.
If you get recommendations from IntelliJ to use string templates, you either need to update IntelliJ (it shouldn't suggest this at all for Java 23), or possibly you have set your language level to "21 (Preview)" or "22 (Preview)", instead of normal (non-preview) language level "21", "22" or "23". In other words, go to File, Project Structure, and on Project check the language level, and double-check for individual Modules (on their "Sources" tab).
As an aside, recent Java versions already optimize string concatenation like "The capital city of " + index + " is " pretty well by replacing it with a low-level form of templating in the compiled bytecode.
java - Why is String template not available - Stack Overflow
There will be no String Template in JDK 23.
String replacement in java, similar to a velocity template - Stack Overflow
JEP: String Templates (Final) for Java 22
Videos
The string template was a preview feature in Java 21 and Java 22, and has been withdrawn for further redesign. In other words, this feature never existed in mainline Java (you needed to enable preview features to use it), and now with the release of Java 23, it doesn't exist at all.
If you get recommendations from IntelliJ to use string templates, you either need to update IntelliJ (it shouldn't suggest this at all for Java 23), or possibly you have set your language level to "21 (Preview)" or "22 (Preview)", instead of normal (non-preview) language level "21", "22" or "23". In other words, go to File, Project Structure, and on Project check the language level, and double-check for individual Modules (on their "Sources" tab).
As an aside, recent Java versions already optimize string concatenation like "The capital city of " + index + " is " pretty well by replacing it with a low-level form of templating in the compiled bytecode.
String Templates were a preview feature of Java 21/22 and were removed in Java 23. The reason given was:
After some experience with the prototype, it became clear that the processor-centric nature of the proposed design was confusing to users and lacked the desired compositionality.
Earlier versions of IntelliJ (2024.1?) were not yet fully aware of all the features of Java 23. As of recent versions (2024.2.2 or possibly earlier) it is now fully compatible with Java 23 and will no longer suggest string templates.
"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
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);
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);