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
🌐
W3Schools
w3schools.com › java › ref_string_replace.asp
Java String replace() Method
Java Examples Java Videos Java ... 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....
🌐
Baeldung
baeldung.com › home › java › java string › remove or replace part of a string in java
Remove or Replace Part of a String in Java| Baeldung
June 15, 2024 - The “(?=<)” pattern asserts that the current position in the input String is followed by the character ‘<‘. However, it doesn’t include ‘<‘ in the overall match. We’ve previously witnessed the effectiveness of regex-based replacement. This problem isn’t a challenge for regex-based replacement either. We can use replaceFirst() to solve this problem. replaceFirst() works pretty similar to replaceAll(). The difference is, as its name tells, replaceFirst() applies the substitution on the match only once.
🌐
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.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-replace-method-explained-aa35534af671
Java’s String.replace() Explained | Medium
October 7, 2024 - The String.replace() method in Java allows you to replace all occurrences of a specific character or substring with another character or substring.
🌐
Apache Commons
commons.apache.org › proper › commons-text › javadocs › api-release › org › apache › commons › text › StringSubstitutor.html
StringSubstitutor (Apache Commons Text 1.9 API)
Substitutes variables within a string by values. This class takes a piece of text and substitutes all the variables within it. The default definition of a variable is ${variableName}. The prefix and suffix can be changed via constructors and set methods. Variable values are typically resolved ...
🌐
Javatpoint
javatpoint.com › java-string-replace
Java String replace()
Java String replace() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string replace in java etc.
Find elsewhere
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › org › apache › commons › lang3 › text › StrSubstitutor.html
StrSubstitutor (Apache Commons Lang 3.20.0 API)
Substitutes variables within a string by values. This class takes a piece of text and substitutes all the variables within it. The default definition of a variable is ${variableName}. The prefix and suffix can be changed via constructors and set methods. Variable values are typically resolved ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-string-replace-method-java
String replace() method in Java with Examples - GeeksforGeeks
February 16, 2024 - It returns a string derived from this string by replacing every occurrence of oldch with newch. NullPointerException- replace() method returns this exception when the target char/CharSequence is null.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
2 weeks ago - String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.
🌐
Career Karma
careerkarma.com › blog › java › java string replace() method: a step-by-step guide
Java String replace() Method: A Step-By-Step Guide | Career Karma
December 1, 2023 - We can use the replace() method to perform such a change. The Java string replace() method is used to replace all instances of a particular character or set of characters in a string with a replacement string.
🌐
Scaler
scaler.com › home › topics › java string interpolation
Java String Interpolation - Scaler Topics
December 20, 2022 - In this case, the format string contains two placeholders %s, which indicate that two string arguments will be substituted into the string. The first %s is replaced with the value of str1, and the second %s is replaced with the value of str2. In this section, we will learn how to achieve java string interpolation using the MessageFormat Class.
🌐
Baeldung
baeldung.com › home › java › java string › java string.replace()
Java.String.replace() | Baeldung
April 11, 2025 - • Java String.String() • Java ... • Java String.valueOf() The method replace() replaces all occurrences of a String in another String or all occurrences of a char with another char....
🌐
CodeGym
codegym.cc › java blog › strings in java › java string replace() method
Java String replace() Method
December 24, 2024 - // Case sensitivity String caseTest ... bnn · The Java String replaceAll() method replaces EVERY single occurrence of a regular expression passed as a parameter with the desired String....
🌐
TutorialsPoint
tutorialspoint.com › substitute-tokens-in-a-string-in-java
Substitute tokens in a String in Java
Let us see an example to substitute tokens in Java − ... import java.text.MessageFormat; public class Example { public static void main(String[] args) { Object[] obj = new Object[] { "Good", "!!"}; // The Morning argument has the placeholders 0 and 1 so it lies between Good and !!
🌐
Jsparrow
jsparrow.github.io › rules › replace-string-format-by-formatted.html
Replace String Format by Formatted | jSparrow Documentation
This rule replaces invocations of the static method `String.format` by invocations of the instance method `String.formatted`.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › replace
Java String replace() - Replace Substrings | Vultr Docs
December 18, 2024 - The replace() method in Java is an essential tool for string manipulation, allowing developers to replace occurrences of a substring or character within a string with a new substring or character.
🌐
Programiz
programiz.com › java-programming › library › string › replace
Java String replace()
class Main { public static void main(String[] args) { String str1 = "abc cba"; // all occurrences of 'a' is replaced with 'z' System.out.println(str1.replace('a', 'z')); // zbc cbz // all occurences of 'L' is replaced with 'J' System.out.println("Lava".replace('L', 'J')); // Java // character not in the string