Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation in Java 14 or 15

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}");
like image 778
San Jaisy Avatar asked Aug 24 '20 10:08

San Jaisy


People also ask

Is there string interpolation in Java?

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.

Can I use string interpolation?

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.

Which is the correct syntax of string interpolation?

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.

Which strings do not interpolate variables?

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.


3 Answers

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.

like image 138
Brian Goetz Avatar answered Oct 10 '22 20:10

Brian Goetz


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.

like image 11
GhostCat Avatar answered Oct 10 '22 22:10

GhostCat


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"
like image 2
Unmitigated Avatar answered Oct 10 '22 22:10

Unmitigated