How to bind data in heredoc scala?

val name = "mike" val str = """Hi, {name}!""" println(str) 

I want it to output str as Hi, mike! but failed. How to do it?

+3
scala heredoc rawstring
source share
3 answers

Scala does not support line interpolation. There is a compiler plugin that implements it at http://github.com/jrudolph/scala-enhanced-strings .

Without a plugin, you can use concatenation or formatting strings:

 val str = name formatted "Hi, %s!" 

or of course

 val str = "Hi, %s!".format(name) 
+5
source share

A common hacker solution will be to use Scala XML interpolation:

 val name = "Mike" val str = <a>Hi, {name}!</a> text 

The text method returns the contents of the string of the XML construct, so our tags are deleted.

+5
source share

As with scala> = 2.10, string interpolations are supported:

 val str = "Foo Bar" str: String = Foo Bar scala> s"Interpolating: $str" res0: String = Interpolating: Foo Bar 
+3
source share

All Articles