Scala | String Interpolation - GeeksforGeeks

26 Feb, 2019
String Interpolation refers to substitution of defined variables or expressions in a given String with respected values. String Interpolation provides an easy way to process String literals. To apply this feature of Scala, we must follow few rules:
String must be defined with starting character as s / f /raw.Variables in the String must have ‘$’ as prefix.Expressions must be enclosed within curly braces ({, }) and ‘$’ is added as prefix.
String must be defined with starting character as s / f /raw.
Variables in the String must have ‘$’ as prefix.
Expressions must be enclosed within curly braces ({, }) and ‘$’ is added as prefix.
Syntax:
// x and y are defined
val str = s"Sum of $x and $y is ${x+y}"
s Interpolator: Within the String, we can access variables, object fields, functions calls, etc.Example 1: variables and expressions:// Scala program// for s interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 20        val y = 10              // without s interpolator        val str1 = "Sum of $x and $y is ${x+y}"          // with s interpolator        val str2 = s"Sum of $x and $y is ${x+y}"              println("str1: "+str1)        println("str2: "+str2)    }}Output:str1: Sum of $x and $y is ${x+y}
str2: Sum of 20 and 10 is 30Example 2: function call// Scala program// for s interpolator  // Creating objectobject GFG{     // adding two numbers    def add(a:Int, b:Int):Int    =    {         a+b     }          // Main method    def main(args:Array[String])    {              val x = 20        val y = 10              // without s interpolator        val str1 = "Sum of $x and $y is ${add(x, y)}"          // with s interpolator        val str2 = s"Sum of $x and $y is ${add(x, y)}"              println("str1: " + str1)        println("str2: " + str2)    }}Output:str1: Sum of $x and $y is ${add(x, y)}
str2: Sum of 20 and 10 is 30f Interpolator: This interpolation helps in formatting numbers easily.To understand how format specifiers work refer Format Specifiers.Example 1: printing upto 2 decimal place:// Scala program// for f interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 20.6              // without f interpolator        val str1 = "Value of x is $x%.2f"          // with f interpolator        val str2 = f"Value of x is $x%.2f"              println("str1: " + str1)        println("str2: " + str2)          }}Output:str1: Value of x is $x%.2f
str2: Value of x is 20.60Example 2: setting width in integers:// Scala program// for f interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 11              // without f interpolator        val str1 = "Value of x is $x%04d"          // with f interpolator        val str2 = f"Value of x is $x%04d"              println(str1)        println(str2)    }}Output:Value of x is $x%04d
Value of x is 0011If we try to pass a Double value while formatting is done using %d specifier, compiler outputs an error. In case of %f specifier, passing Int is acceptable.raw Interpolator: String Literal should start with ‘raw’. This interpolator treats escape sequences same as any other character in a String.Example :printing escape sequence:// Scala program// for raw interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {                  // without raw interpolator        val str1 = "Hello\nWorld"          // with raw interpolator        val str2 = raw"Hello\nWorld"              println("str1: " + str1)        println("str2: " + str2)    }}Output:str1: Hello
World
str2: Hello\nWorld
s Interpolator: Within the String, we can access variables, object fields, functions calls, etc.Example 1: variables and expressions:// Scala program// for s interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 20        val y = 10              // without s interpolator        val str1 = "Sum of $x and $y is ${x+y}"          // with s interpolator        val str2 = s"Sum of $x and $y is ${x+y}"              println("str1: "+str1)        println("str2: "+str2)    }}Output:str1: Sum of $x and $y is ${x+y}
str2: Sum of 20 and 10 is 30Example 2: function call// Scala program// for s interpolator  // Creating objectobject GFG{     // adding two numbers    def add(a:Int, b:Int):Int    =    {         a+b     }          // Main method    def main(args:Array[String])    {              val x = 20        val y = 10              // without s interpolator        val str1 = "Sum of $x and $y is ${add(x, y)}"          // with s interpolator        val str2 = s"Sum of $x and $y is ${add(x, y)}"              println("str1: " + str1)        println("str2: " + str2)    }}Output:str1: Sum of $x and $y is ${add(x, y)}
str2: Sum of 20 and 10 is 30
Example 1: variables and expressions:
// Scala program// for s interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 20        val y = 10              // without s interpolator        val str1 = "Sum of $x and $y is ${x+y}"          // with s interpolator        val str2 = s"Sum of $x and $y is ${x+y}"              println("str1: "+str1)        println("str2: "+str2)    }}
Output:
str1: Sum of $x and $y is ${x+y}
str2: Sum of 20 and 10 is 30
Example 2: function call
// Scala program// for s interpolator  // Creating objectobject GFG{     // adding two numbers    def add(a:Int, b:Int):Int    =    {         a+b     }          // Main method    def main(args:Array[String])    {              val x = 20        val y = 10              // without s interpolator        val str1 = "Sum of $x and $y is ${add(x, y)}"          // with s interpolator        val str2 = s"Sum of $x and $y is ${add(x, y)}"              println("str1: " + str1)        println("str2: " + str2)    }}
Output:
str1: Sum of $x and $y is ${add(x, y)}
str2: Sum of 20 and 10 is 30
f Interpolator: This interpolation helps in formatting numbers easily.To understand how format specifiers work refer Format Specifiers.Example 1: printing upto 2 decimal place:// Scala program// for f interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 20.6              // without f interpolator        val str1 = "Value of x is $x%.2f"          // with f interpolator        val str2 = f"Value of x is $x%.2f"              println("str1: " + str1)        println("str2: " + str2)          }}Output:str1: Value of x is $x%.2f
str2: Value of x is 20.60Example 2: setting width in integers:// Scala program// for f interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 11              // without f interpolator        val str1 = "Value of x is $x%04d"          // with f interpolator        val str2 = f"Value of x is $x%04d"              println(str1)        println(str2)    }}Output:Value of x is $x%04d
Value of x is 0011If we try to pass a Double value while formatting is done using %d specifier, compiler outputs an error. In case of %f specifier, passing Int is acceptable.
To understand how format specifiers work refer Format Specifiers.
Example 1: printing upto 2 decimal place:
// Scala program// for f interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 20.6              // without f interpolator        val str1 = "Value of x is $x%.2f"          // with f interpolator        val str2 = f"Value of x is $x%.2f"              println("str1: " + str1)        println("str2: " + str2)          }}
Output:
str1: Value of x is $x%.2f
str2: Value of x is 20.60
Example 2: setting width in integers:
// Scala program// for f interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {              val x = 11              // without f interpolator        val str1 = "Value of x is $x%04d"          // with f interpolator        val str2 = f"Value of x is $x%04d"              println(str1)        println(str2)    }}
Output:
Value of x is $x%04d
Value of x is 0011
If we try to pass a Double value while formatting is done using %d specifier, compiler outputs an error. In case of %f specifier, passing Int is acceptable.
raw Interpolator: String Literal should start with ‘raw’. This interpolator treats escape sequences same as any other character in a String.Example :printing escape sequence:// Scala program// for raw interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {                  // without raw interpolator        val str1 = "Hello\nWorld"          // with raw interpolator        val str2 = raw"Hello\nWorld"              println("str1: " + str1)        println("str2: " + str2)    }}Output:str1: Hello
World
str2: Hello\nWorld
// Scala program// for raw interpolator  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {                  // without raw interpolator        val str1 = "Hello\nWorld"          // with raw interpolator        val str2 = raw"Hello\nWorld"              println("str1: " + str1)        println("str2: " + str2)    }}
Output:
str1: Hello
World
str2: Hello\nWorld
Picked
Scala
Scala-Strings
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
For Loop in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Scala | map() method
Scala | flatMap Method
Scala List filter() method with example
Scala | reduce() Function
String concatenation in Scala
Type Casting in Scala
Scala List contains() method with example
Scala String substring() method with example