String literals in Kotlin are capable of string interpolation, and the dollar sign is the start of a string template expression. If you need the literal dollar sign in a string instead, you should escape it using a backslash: \$. So your template (which I assume you're passing to String.format) becomes:
val template = "Something %s something else %s. The first was %1\$s, the second was %2\$s"
Answer from Alexander Udalov on Stack OverflowString literals in Kotlin are capable of string interpolation, and the dollar sign is the start of a string template expression. If you need the literal dollar sign in a string instead, you should escape it using a backslash: \$. So your template (which I assume you're passing to String.format) becomes:
val template = "Something %s something else %s. The first was %1\$s, the second was %2\$s"
As Alexander Udalov's answer say, $ can be used for String Templates.
Apart from use backslash to escape the char $, you also can use ${'$'} to escape it. This syntax will be more useful when you want to escape the $ in a raw string, where backslash escaping is not supported.
val template = "Something %s something else %s. The first was %1${'$'}s, the second was %2${'$'}s"
Unfortunately, there's no built-in support for formatting in string templates yet, as a workaround, you can use something like:
"pi = ${pi.format(2)}"
the .format(n) function you'd need to define yourself as
fun Double.format(digits: Int) = "%.${digits}f".format(this)
This will work only in Kotlin/JVM.
There's clearly a piece of functionality here that is missing from Kotlin at the moment, we'll fix it.
As a workaround, There is a Kotlin stdlib function that can be used in a nice way and fully compatible with Java's String format (it's only a wrapper around Java's String.format())
See Kotlin's documentation
Your code would be:
val pi = 3.14159265358979323
val s = "pi = %.2f".format(pi)