This is just one way Kotlin improves on Java.
In Java, you can use String.format()
, like so:
System.out.println("".format("Hello, %s!", "world"));
This works because the static format()
method can be called using a String reference which is why we start the expression with an empty string, "". The format string needs to be passed in as the first argument and there needs to be enough subsequent arguments to match all the format placeholders.
In Kotlin, it's much simpler and more object-oriented. You can call format on a String and the string itself will be treated as the format string. You only need to pass in enough arguments to match all the format placeholders.
println "Hello, %s!".format("world")