How to repeat argument in String format in Scala

java, scala, string-formatting

Solution

This should work:

"%1$s-%1$s-%1$s" format "OK"

The `format` method of WrappedString uses `java.util.Formatter` under the hood. And the the Formatter Javadoc says:

The format specifiers for general, character, and numeric types have the following syntax:

%[argument_index$][flags][width][.precision]conversion

The optional `argument_index` is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by `"1$"`, the second by `"2$"`, etc.

Problem

How to reuse the same string for format placement? e.g ``` "%s-%s-%s" format("OK") >> "OK-OK-OK" ```

Original source