Print a variable as part of a multiline string?

scala

Solution

If you're on 2.10 or later, you can use string interpolation:

scala> s"""
     |   |Hi! My name is $name how are you?
     | """.stripMargin
res0: String = 
"
Hi! My name is Cory how are you?
"

For 2.9 or earlier you're stuck with something like this:

scala> ("""
     |   |Hi! My name is """ + name + """ how are you?
     | """).stripMargin
res1: String = 
"
Hi! My name is Cory how are you?
"

Note that there are several flavors of string interpolation in Scala—`s"..."` is the simplest.

Problem

``` val name = "Cory" """ |Hi! My name is " + name + " how are you? """.stripMargin ``` The portion `+ name +` doesn't get interpreted as code, but just as text. How can I print the value of a variable inside a multiline string?

Original source