String interpolation is a mechanism to combine your values inside a string with variables.
The Scala notation for string interpolation is an “s” prefix added before the first double quote of the string.
Then dollar sign operators ($) (with optional braces) can be used to note references to external data.
String Interpolation Example
Here is an example of String Interpolation to print greeting message to the user:
val name = "John";
When using interpolated strings, to write a literal dollar sign $, use two of them, $$.
We can also use it printf notation for String interpolation.
To use printf notation change the prefix to an “f ” and follow the end of the reference immediately with the printf notation:
val name = "John Doe";
String Interpolation : f vs s
f interpolation is typesafe.
Also, it allows to add formatting right after the parameter, which s interpolation doesn’t support.
© 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post