Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enhanced MessageFormat?

Tags:

java

I've been using MessageFormat.format() for a while, but there's one thing that I find annoying.

Everytime you declare a parameter in a message, you have to know the position of the mapped argument. e.g.

MessageFormat.format("{0} is annoying {1}.", "this", "indeed")

Is there a class that is the same as MessageFormat in every other way but lets you omit the argument position in parameter declaration altogether and have it default to its position in the message so that the first parameter maps to the first argument, the second parameter to the second argument and so on? e.g.

MessageFormat.format("{} is much better{}.", "this", "indeed")

I think later versions of log4j have a similar feature, but I just need the formatting class.

Happy New Year!

EDIT: I need this feature for assertion, so it's really for internal use, but I appreciate your insight into why MessageFormat works the way it does.

like image 589
Tom Tucker Avatar asked Dec 03 '22 10:12

Tom Tucker


1 Answers

You should be using Formatter or its frontend, String.format, instead. You can do:

String.format("%s is much better %s", "this", "indeed");
String.format("%1$s provides positional formatting %2$s", "this", "indeed");
like image 62
Chris Jester-Young Avatar answered Dec 21 '22 20:12

Chris Jester-Young