How to reuse the same string for format placement? e.g
"%s-%s-%s" format("OK")
>> "OK-OK-OK"
In Scala, as in Java, a string is an immutable object, that is, an object that cannot be modified. On the other hand, objects that can be modified, like arrays, are called mutable objects. Strings are very useful objects, in the rest of this section, we present important methods of java. lang. String class.
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.
"%s-%s-%s".format(Seq.fill(3)("OK"): _*)
The : _*
part means "use this sequence as the arguments". Seq.fill(3)("OK")
creates three copies of "OK"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With