Kotlin, how to use '$' as argument index in String.format

Viewed 4774

In kotlin how to use the '$' for argument index in the string for formatting. i.e. would like put the same number 122 at first position and 3rd position of the string. In kotlin the string does not like the '$' for this.

(note: the strFormat are dynamically generated and can't be stored in the string resource file).

val strFormat = "%d, %s, same as 1st int: %1$d, same as 1st string: %1$s, $3$s"
val str = String.format(strFormat, 122, "the string one", "string two")

enter image description here

1 Answers

$ is a special kotlin metacharacter for string templates. You can use the string templating mechanism itself to insert a literal $:

${'$'}
Related