StringSubstitutor from Apache Commons Text library is a lightweight way of doing this, provided your values are already formatted correctly.
Map<String, String> values = new HashMap<>();
values.put("value", "1");
values.put("column","2");
StringSubstitutor sub = new StringSubstitutor(values, "%(", ")");
String result = sub.replace("There's an incorrect value '%(value)' in column # %(column)");
The result string will contain the following:
There's an incorrect value '1' in column # 2
When using Maven you can add this dependency to your pom.xml:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
Answer from schup on Stack OverflowStringSubstitutor from Apache Commons Text library is a lightweight way of doing this, provided your values are already formatted correctly.
Map<String, String> values = new HashMap<>();
values.put("value", "1");
values.put("column","2");
StringSubstitutor sub = new StringSubstitutor(values, "%(", ")");
String result = sub.replace("There's an incorrect value '%(value)' in column # %(column)");
The result string will contain the following:
There's an incorrect value '1' in column # 2
When using Maven you can add this dependency to your pom.xml:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
not quite, but you can use MessageFormat to reference one value multiple times:
MessageFormat.format("There's an incorrect value \"{0}\" in column # {1}", x, y);
The above can be done with String.format() as well, but I find messageFormat syntax cleaner if you need to build complex expressions, plus you dont need to care about the type of the object you are putting into the string
See String.format method.
String s = "hello %s!";
s = String.format(s, "world");
assertEquals(s, "hello world!"); // should be true
StrSubstitutor from Apache Commons Lang may be used for string formatting with named placeholders:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.1</version>
</dependency>
https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/text/StrSubstitutor.html :
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 from a map, but could also be resolved from system properties, or by supplying a custom variable resolver.
Example:
String template = "Hi ${name}! Your number is ${number}";
Map<String, String> data = new HashMap<String, String>();
data.put("name", "John");
data.put("number", "1");
String formattedString = StrSubstitutor.replace(template, data);
Imagine the following piece of code
Template("Hello {name}, you are {position} in line").format(name="Alice", position=12, extra=42) Would you rather that an error be thrown due to an unbound key (extra) being passed or should it be ignored?
I didn't find much languages even having such string templating built in except other than Python (string.format) and Rust (format!), in Python the extra key/value is ignored, while Rust will throw an exception.
What would be your approach?