String.format

String str = String.format("Action %s occured on object %s.",
   objectA.getAction(), objectB);

Or

String str = String.format("Action %s occured on object %s with outcome %s.",
   new Object[]{objectA.getAction(), objectB, outcome});

You can also use numeric positions, for example to switch the parameters around:

String str = String.format("Action %2$s occured on object %1$s.",
   objectA.getAction(), objectB);
Answer from The Scrum Meister on Stack Overflow
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-2.6 › org › apache › commons › lang › text › StrSubstitutor.html
StrSubstitutor (Commons Lang 2.6 API) - Apache Commons
This method allows a string suffix to be easily set. ... Gets the VariableResolver that is used to lookup variables. ... Sets the VariableResolver that is used to lookup variables. ... Returns a flag whether substitution is done in variable names. ... Sets a flag whether substitution is done in variable names. If set to true, the names of variables can contain other variables which are processed first before the original variable is evaluated, e.g. ${jre-${java...
🌐
Baeldung
baeldung.com › home › java › java string › string interpolation in java
String Interpolation in Java | Baeldung
July 23, 2025 - @Test public void givenTwoString_thenInterpolateWithStringSubstitutor() { String EXPECTED_STRING = "String Interpolation in Java with some Java examples."; String baseString = "String ${first} in ${second} with some ${second} examples."; String first = "Interpolation"; String second = "Java"; Map<String, String> parameters = new HashMap<>(); parameters.put("first", first); parameters.put("second", second); StringSubstitutor substitutor = new StringSubstitutor(parameters); String result = substitutor.replace(baseString); assertEquals(EXPECTED_STRING, result); } From our code example, we can see that we created a Map. The key names are the same as the names of the variables that we’ll replace in the String.
🌐
Apache Commons
commons.apache.org › proper › commons-text › apidocs › org › apache › commons › text › StringSubstitutor.html
StringSubstitutor (Apache Commons Text 1.15.0 API)
For example: ... In some complex scenarios you might even want to perform substitution in the names of variables, for instance ... StringSubstitutor supports this recursive substitution in variable names, but it has to be enabled explicitly by calling setEnableSubstitutionInVariables(boolean) ...
🌐
Clapper
software.clapper.org › javautil › api › org › clapper › util › text › VariableSubstituter.html
VariableSubstituter - Software at clapper.org
context - an optional context object, passed through unmodified to the deref object's VariableDereferencer.getVariableValue(java.lang.String, java.lang.Object) method. This object can be anything at all (and, in fact, may be null if you don't care.) It's primarily useful for passing context information from the caller to the VariableDereferencer. ... The (possibly) expanded string. ... substitute(String,VariableDereferencer,Object), VariableDereferencer.getVariableValue(String,Object)
🌐
Simplesolution
simplesolution.dev › java-substitute-a-string-in-java-by-replace-variables-map-to-template-string
Substitute a String in Java by replace variables map to Template String using Apache Commons Text
The StringSubstitutor class support ... static void main(String[] args) { // Template String String templateString = "Hello ${username}, you have ${count:-0} messages."; // Prepare value map of variables to replace to template ...
🌐
Apache Commons
commons.apache.org › proper › commons-text › javadocs › api-release › org › apache › commons › text › StringSubstitutor.html
StringSubstitutor (Apache Commons Text 1.9 API)
For example: ... In some complex scenarios you might even want to perform substitution in the names of variables, for instance ... StringSubstitutor supports this recursive substitution in variable names, but it has to be enabled explicitly by calling setEnableSubstitutionInVariables(boolean) ...
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › string › java text block formatting with variables/expressions
Java Text Block Formatting with Variables/Expressions
January 10, 2024 - The variable name is concatenated directly next to “Hello, ” without any space or newline character. Similarly, “Welcome to Java text blocks!” is concatenated immediately after the name without any new line or space.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-release › org › apache › commons › lang3 › text › StrSubstitutor.html
StrSubstitutor (Apache Commons Lang 3.11 API)
The default definition of a variable ... from system properties, or by supplying a custom variable resolver. The simplest example is to use this class to replace Java System properties....
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › use-string-format-java-string-output
Java String formatting with the String.format method (like ‘sprintf’) | alvinalexander.com
July 30, 2024 - String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED); Finally, here is an example of how to use multiple variables with the String format method:
🌐
Baeldung
baeldung.com › home › java › java string › named placeholders in string formatting
Named Placeholders in String Formatting | Baeldung
August 27, 2025 - To bypass this problem, we can choose different prefixes and suffixes so that they don’t get interfered with: String TEMPLATE = "Text: [%{text}] Number: [%{number}] Text again: [%{text}]"; Map<String, Object> params = new HashMap<>(); ...
🌐
Eclipse
help.eclipse.org › latest › topic › org.eclipse.jdt.doc.user › reference › preferences › run-debug › ref-string_substitution.htm
String Substitution Preferences
Java Development User Guide > Reference > Preferences > Run/Debug · The following preferences can be set using the Run/Debug > String Substitution preference page. This preference page is used solely for the purpose of creating string variables to be reused in other locations that accept string ...
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › org › apache › commons › lang3 › text › StrSubstitutor.html
StrSubstitutor (Apache Commons Lang 3.20.0 API)
The default definition of a variable ... from system properties, or by supplying a custom variable resolver. The simplest example is to use this class to replace Java System properties....
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › replace
Java String replace() - Replace Substrings | Vultr Docs
December 18, 2024 - Utilize the replace() method for replacing single characters. ... String example = "Java"; String updatedExample = example.replace('a', 'o'); System.out.println(updatedExample); Explain Code
🌐
W3Schools
w3schools.com › java › ref_string_replace.asp
Java String replace() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.
🌐
Programmersought
programmersought.com › article › 27692648907
Java template variable substitution - The string replacer - Programmer Sought
Map valuesMap = new HashMap(); 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); Output:The quick brown fox jumped over the lazy dog. You can set default values ​​for variables in the format:${undefined.number:-1234567890},among them undefined.numberIs a variable name,:-It is a delimiter,1234567890It is the default.