Tutorial by Examples

The s interpolator allows the usage of variables within a string. val name = "Brian" println(s"Hello $name") prints "Hello Brian" to the console when ran.
val num = 42d Print two decimal places for num using f println(f"$num%2.2f") 42.00 Print num using scientific notation using e println(f"$num%e") 4.200000e+01 Print num in hexadecimal with a println(f"$num%a") 0x1.5p5 Other format strings can be found ...
You can use curly braces to interpolate expressions into string literals: def f(x: String) = x + x val a = "A" s"${a}" // "A" s"${f(a)}" // "AA" Without the braces, scala would only interpolate the identifier after the $ (in this case f...
It is possible to define custom string interpolators in addition to the built-in ones. my"foo${bar}baz" Is expanded by the compiler to: new scala.StringContext("foo", "baz").my(bar) scala.StringContext has no my method, therefore it can be provided by implicit c...
It is also possible to use Scala's string interpolation feature to create elaborate extractors (pattern matchers), as perhaps most famously employed in the quasiquotes API of Scala macros. Given that n"p0${i0}p1" desugars to new StringContext("p0", "p1").n(i0), it is p...
You can use the raw interpolator if you want a String to be printed as is and without any escaping of literals. println(raw"Hello World In English And French\nEnglish:\tHello World\nFrench:\t\tBonjour Le Monde") With the use of the raw interpolator, you should see the following printed in the ...

Page 1 of 1