Skip to content
Advertisement

Dynamically formatting a string

Before I wander off and roll my own I was wondering if anyone knows of a way to do the following sort of thing…

Currently I am using MessageFormat to create some strings. I now have the requirement that some of those strings will have a variable number of arguments.

For example (current code):

JavaScript

Now I need something like:

JavaScript

and:

JavaScript

There are a couple ways I can think of creating the format string, such as having 1 String per number of arguments (there is a finite number of them so this is practical, but seems bad), or build the string dynamically (there are a lot of them so this could be slow).

Any other suggestions?

Advertisement

Answer

Unless, I’m missing something this is plain old join. Until Java 7 gets String.join (no joke) there are some implementations around like Apache commons lang StringUtils.join.

JavaScript

The only problem is that is does not work on primtive int[] arrays directly.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement