This is the example of String Interpolation that allows users to embed variable references directly in processed string literals. For e.g.
scala> val name = "Scala"
name: String = Scala
scala> println(s"Hello, $name")
Hello, Scala
In above example the literal s"Hello, $name"
is a processed string literal.
Scala provides three string interpolation methods out of the box: s
, f
and raw
.
Prepending f
to any string literal allows the creation of simple formatted strings, similar to printf
in other languages.
The formats allowed after the %
character tells that result is formatted as a decimal number while ${}
allows any arbitrary expression to be embedded. For e.g.
scala> println(s"1 + 1 = ${1 + 1}")
1 + 1 = 2
More detailed information can be found on: