views:

230

answers:

2

I have a property-file with strings inside, formatted in this way:

audit.log.events.purged=The audit events were purged. {0} events were purged, {1} assets were deleted.

Is there a way to bind some values inside those {0} and {1}, using some standard APIs, or should I create some code for parsing those Strings?

+7  A: 

You are looking for MessageFormat: http://java.sun.com/j2se/1.5.0/docs/api/java/text/MessageFormat.html

Confusion
+7  A: 

Java 1.4.2:

String formattedMessage = MessageFormat.format(message, new Object[]{parm1, parm2});

Java 1.5+:

String formattedMessage = MessageFormat.format(message, parm1, parm2);

In both cases you can have as many parameters as you wish, not just two.

Chris Carruthers
Actually, it will work as MessageFormat.format(message, parm1, parm2); as well, cause it accepts enumerations.
folone
Correct - I'm unfortunately still far too used to programming with 1.4's retrictions!
Chris Carruthers