开发者

Java String formatting

开发者 https://www.devze.com 2022-12-14 23:17 出处:网络
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.

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 cod开发者_开发知识库e for parsing those Strings?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消