What classes do you use to make string placeholders work?
String template = "You have %1 tickets for %d",
Brr object = new Brr(template, {new Integer(1), new Date()});
object.print();
What classes do you use to make string placeholders work?
String template = "You have %1 tickets for %d",
Brr object = new Brr(template, {new Integer(1), new Date()});
object.print();
How about java.util.Formatter
?
Shorthands for it include String.format
and System.out.format
.
String.format
is the easiest:
String s = String.format("%s %s", "Hello", "World!");
You can call it with a variable number of parameters, like I showed above, or pass it an array of Object
and it'll use that.
The following should work:
import java.util.*;
class Brr {
String template;
Object[] args;
public Brr(String template, Object... args) {
this.template = template;
this.args = args;
}
public void print() {
System.out.println(String.format(template, args));
}
}
public class Test {
public static void main(String... args) {
String template = "You have %d tickets for %tF";
Brr object = new Brr(template, new Integer(1), new Date());
object.print();
}
}
Output:
You have 1 tickets for 2010-06-01
Have a look at http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html if you want the full reference of conversions.
MessageFormat.format()
allows me to use ordinal parameters, thus easily enabling i18n
private final Map<String, String> localizedMessages = new HashMap<String, String>();
private void init() {
this.localizedMessages.put("de_DE", "{2} Suchtreffer, zeige Ergebnisse ${0} bis ${1}");
this.localizedMessages.put("en_US", "Showing results {0} through {1} of a total {2");
}
public String getLocalizedMessage(final String locale,
final Integer startOffset, final Integer endOffset,
final Integer totalResults) {
return MessageFormat.format(this.localizedMessages.get(locale),
startOffset, endOffset, totalResults);
}
You have two options:
java.util.Formatter
printf
-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output.java.text.MessageFormat
.
MessageFormat
provides a means to produce concatenated messages in a language-neutral way. Use this to construct messages displayed for end users. Of the two, MessageFormat
is by far the more powerful. Here's an example of using ChoiceFormat
to handle 0
, 1
, and >1
case differently:
import java.text.MessageFormat;
import java.util.Date;
//...
String p = "You have {0,choice,0#none|1#one ticket|1<{0,number,integer} tickets} for {1,date,full}.";
for (int i = 0; i < 4; i++) {
System.out.println(MessageFormat.format(p, i, new Date()));
}
This prints:
You have none for Tuesday, June 1, 2010.
You have one ticket for Tuesday, June 1, 2010.
You have 2 tickets for Tuesday, June 1, 2010.
You have 3 tickets for Tuesday, June 1, 2010.
The documentation has many more examples.
If you need something a little more powerful for templating strings the Apache Velocity library is pretty useful http://velocity.apache.org/