Ok, so we all should know that you can include variables into strings by doing:
String string = "A string " + aVariable;
Is there a way to do it like:
String string = "A string {aVariable}";
In other words: Without having to close the quotation marks and adding plus signs. It's very unattractive.
Also consider
java.text.MessageFormat
, which uses a related syntax having numeric argument indexes. For example,results in
string
containing the following:More commonly, the class is used for its numeric and temporal formatting. An example of
JFreeChart
label formatting is described here; the classRCInfo
formats a game's status pane.This is called string interpolation; it doesn't exist as such in Java.
One approach is to use String.format:
Another approach is to use a templating library such as Velocity or FreeMarker.
You can always use String.format(....). i.e.,
I'm not sure if that is attractive enough for you, but it can be quite handy. The syntax is the same as for printf and java.util.Formatter. I've used it much especially if I want to show tabular numeric data.