Take the 2-minute tour ×
val name = "mike"
val str = """Hi, {name}!"""

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

share|improve this question
val str = "Hi, " + name + "!" ? –  pascal Jul 26 '10 at 17:19
Similar question stackoverflow.com/questions/2183503/… –  pascal Jul 26 '10 at 17:29

3 Answers 3

up vote 7 down vote accepted

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

Without the plugin you can use concatenation or format strings:

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

or of course

val str = "Hi, %s!".format(name)
share|improve this answer
@Moritz, thank you for your answer. I don't understand why scala not support it heredoc, since they support it in xml –  Freewind Jul 26 '10 at 17:47
@Freewind in XML literals the curly braces are actually just a fancy syntax for inserting an instance of scala.xml.Text with the string value of the block into the XML nodes. XML is a bit special here but Strings are just Strings to the compiler. –  Moritz Jul 26 '10 at 18:05
@Freewind Also see this discussion on why Scala doesn't support string interpolation: goo.gl/cJ5u –  missingfaktor Jul 26 '10 at 19:15
You can also make format more Pythonic with an implicit: bubblefoundry.com/blog/2010/06/fun-with-scala-implicits –  pr1001 Jul 26 '10 at 21:34
@Freewind, Yes, I am able to open it. You can try this direct link if the shortened URL isn't working: scala-programming-language.1934581.n4.nabble.com/… –  missingfaktor Jul 27 '10 at 12:31

A total hackish solution would be to use Scala's XML interpolation:

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

The text method returns string contents of an XML construct, so our tags are stripped.

share|improve this answer
+1 This is basically a "canonical" hack. Maybe being so accepted (and useful) this is not really a "hack" anymore?? –  javadba May 26 at 18:20

As of scala >= 2.10 string interpolations are supported:

val str = "Foo Bar"
str: String = Foo Bar

scala> s"Interpolating: $str"
res0: String = Interpolating: Foo Bar
share|improve this answer

Your Answer


