Since I am using Java 14 and 15 preview features. Trying to find the string interpolation in java.
The closest answer I found is
String.format("u1=%s;u2=%s;u3=%s;u4=%s;", u1, u2, u3, u4)
Since the answer which I got from lots fo references are old answers asked 4,5 years ago. Is there any update on the string interpolation in java 11,12,13,14,15 equivalent to C#
string name = "Horace";
int age = 34;
Console.WriteLine($"Your name is {name} and your age {age}");
String Interpolation replaces the placeholder with the mentioned variable names assigned to strings and hence makes it efficient to write large variable names or text. String Interpolation in Java can be done in several ways with some concatenation operator or built-in functions or classes.
Beginning with C# 10, you can use string interpolation to initialize a constant string. All expressions used for placeholders must be constant strings. In other words, every interpolation expression must be a string, and it must be a compile time constant.
Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.
Interpolation: The variable parsing is allowed when the string literal is enclosed with double quotes or with heredocs. Single quoted string or nowdocs, does not supports variable interpolation.
There is something slightly closer; an instance version of String::format
, called formatted
:
String message = "Hi, %s".formatted(name);
It is similar to String::format
, but is more friendly to use in chained expressions.
To my knowledge, there are no updates in the standard java libraries regarding such kind of string formatting.
In other words: you are still "stuck" with either using String.format()
and its index based substitution mechanism, or you have to pick some 3rd party library/framework, such as Velocity, FreeMarker, ... see here for an initial overview.
There is no built-in support for that currently, but Apache Commons StringSubstitutor
can be used.
import org.apache.commons.text.StringSubstitutor;
import java.util.HashMap;
import java.util.Map;
// ...
Map<String, String> values = new HashMap<>();
values.put("animal", "quick brown fox");
values.put("target", "lazy dog");
StringSubstitutor sub = new StringSubstitutor(values);
String result = sub.replace("The ${animal} jumped over the ${target}.");
// "The quick brown fox jumped over the lazy dog."
This class supports providing default values for variables.
String result = sub.replace("The number is ${undefined.property:-42}.");
// "The number is 42."
To use recursive variable replacement, call setEnableSubstitutionInVariables(true);
.
Map<String, String> values = new HashMap<>();
values.put("b", "c");
values.put("ac", "Test");
StringSubstitutor sub = new StringSubstitutor(values);
sub.setEnableSubstitutionInVariables(true);
String result = sub.replace("${a${b}}");
// "Test"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With