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 OverflowW3Schools
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....
Top answer 1 of 6
131
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);
2 of 6
47
You can use String.format or MessageFormat.format
E.g.,
MessageFormat.format("A sample value {1} with a sample string {0}",
new Object[] {"first", 1});
or simply
MessageFormat.format("A sample value {1} with a sample string {0}", "first", 1);
Videos
02:23
Java String replace() Method | How to use String replace () in Java?
10:53
How to replace the occurrences of a substring in a given String ...
06:42
Java Tutorial - 18 - Replacing Characters in a String - YouTube
- YouTube
02:08
How to replace a character in String in java? - YouTube
Apache Commons
commons.apache.org › proper › commons-text › apidocs › org › apache › commons › text › StringSubstitutor.html
StringSubstitutor (Apache Commons Text 1.15.0 API)
Internal method that substitutes the variables. ... Returns a string representation of the object.
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.
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.
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
java.lang.Object org.apache.commons.lang.text.StrSubstitutor ... Substitutes variables within a string by values.
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 ...
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.
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.
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`.
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