Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation in Scala?

I wanted to ask if there is any type of string interpolation in Scala. I have made a search on the subject but 'till now I have found that there is no string interpolation. Is there plans to get implemented in next versions?

Thanks!

UPDATE

String interpolation is going to be in scala 2.10 which you can try out since scala 2.10.RC1 is out (20/10/2012). You can check out this SIP for scala 2.11 which states that interpolated strings in the pattern matcher will be valid syntax. With the new string interpolation you can do something like this:

val age = 28
val name = "Gerry"

s"My name is $name and I am $age years old"
res0: String = My name is Gerry and I am 28 years old

But try out the documentation on all the interpolators that are available at the moment. Note that you can define your own interpolators! Try this link for more info.

like image 694
Gerry Avatar asked Oct 27 '11 14:10

Gerry


People also ask

What is string interpolation in Scala?

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.

How do you escape a Scala string interpolation?

Interpolations use this escape to splice in arguments, and it can also be used to escape itself as the sequence $$ to represent a literal $ character. Because of its special handling during the parse, the $ could be used to escape a " character to represent a literal " withing a string.

What is the syntax for string interpolation?

Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.

What does string interpolation do?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.


4 Answers

It's not in the (released) scala library yet. But there is a SIP (Scala Improvement Process) for the addition of this feature:

http://docs.scala-lang.org/sips/pending/string-interpolation.html

like image 68
Ben James Avatar answered Oct 06 '22 00:10

Ben James


You can do it C-style:

"Interpolate my %s here" format List(1,2,3)

//String = Interpolate my List(1, 2, 3) here

or

List(1,2,3) formatted "Interpolate my %s here"

You can use these on anything with a toString (i.e. anything)

case class Foo(n: Int)
Foo(42) formatted "Here is a %s !!!!"
//String = Here is a Foo(42) !!!!

although the former is more flexible in terms of enabling multiple interpolations in a single string (since it can take multiple arguments).

like image 29
Luigi Plinge Avatar answered Oct 06 '22 00:10

Luigi Plinge


yes, there is string interpolation in current scala releases via compiler plugin see http://jrudolph.github.com/scala-enhanced-strings

like image 20
OlegYch Avatar answered Oct 06 '22 00:10

OlegYch


I use the xml hack on scala 2.9

val age = 28
val name = "Gerry"

<a>My name is {name} and I am {age} years old</a>.text
res0: String = My name is Gerry and I am 28 years old
like image 37
dips Avatar answered Oct 05 '22 23:10

dips