String interpolation: f or s

I wonder if there is a difference between the two:

val a = 123 println(f"hello1 $a") // 1 println(s"hello1 $a") // 2 
+8
source share
2 answers

According to docs, interpolation f is typical. In addition, it allows you to add formatting immediately after a parameter that s does not support interpolation.

+11
source share

Interpolator s allows you to include variables or expressions, while interpolator f allows you to use formatting commands à la C printf .

Note that contrary to C printf or Java String.format f interpolator will check the parameters for you. This link contains additional information.

+7
source share

All Articles