I find myself very frequently wanting to write reusable strings with parameter placeholders in them, almost exactly like what you'd find in an SQL PreparedStatement.
Here's an example
private static final String warning = "You requested ? but were assigned ? instead.";
public void addWarning(Element E, String requested, String actual){
warning.addParam(0, requested);
warning.addParam(1, actual);
e.setText(warning);
//warning.reset() or something, I haven't sorted that out yet.
}
Does something like this exist already in Java ? Or, is there a better way to address something like this ?
What I'm really asking: is this ideal ?
String.format()
Since Java 5, you can use String.format
to parametrize Strings. Example:
String fs;
fs = String.format("The value of the float " +
"variable is %f, while " +
"the value of the " +
"integer variable is %d, " +
" and the string is %s",
floatVar, intVar, stringVar);
See http://docs.oracle.com/javase/tutorial/java/data/strings.html
Alternatively, you could just create a wrapper for the String
to do something more fancy.
MessageFormat
Per the comment by Max and answer by Affe, you can localize your parameterized String with the MessageFormat
class.
You could use String.format
. Something like:
String message = String.format("You requested %2$s but were assigned %1$s", "foo", "bar");
will generate
"You requested bar but were assigned foo"
It is built-in, yes. The class you're looking for is java.text.MessageFormat
Well if your String was final
you sure wouldn't be able to modify it later. I don't know if maybe you could find a better use case for that kind of thing as you could simply do:
public void addWarning(Element E, String requested, String actual){
String warning = "You requested" + requested + " but were assigned " + actual + " instead."
e.setText(warning);
}
I would probably do something like:
private final String warning = String.format("You requested %s but were assigned %s instead.", requested, actual);
If you wanted to put the parameterized string before the call to format the string you could do something like what you see below, although this is less clear.
Neither of these solutions are inherently localizeable; you may want to consider something like a .properties file if you wish to support non-English locales.
private static final String warning = "You requested %s but were assigned %s instead.";
public void addWarning(Element E, String requested, String actual){
e.setText(String.format(warning, requested, actual);
//warning.reset() or something, I haven't sorted that out yet.
}
the formatter can do this for you (with some extras as adding leading zeroes spacing things out and more)
private static final String warning = "You requested %1$s but were assigned %2$s instead.";
public void addWarning(Element E, String requested, String actual){
Formatter f = new Formatter();//you'll need to recreate it each time
try{
f.format(warning,requested,actual);
e.setText(f.out().toString());
}finally{f.close();}
}
The String class provides the following format method, http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html. For example (as per the original post):
private final static String string = "You requested %s but were assigned %s instead.";
public void addWarning(Element e, String requested, String actual) {
String warning = String.format(string, requested, actual);
e.setText(warning);