Ger*_*rry 15 scala string-interpolation
我想询问Scala中是否存在任何类型的字符串插值.我已经对这个主题进行了搜索,但是到现在为止我发现没有字符串插值.是否有计划在下一版本中实施?
谢谢!
UPDATE
字符串插值将在scala 2.10中,您可以试用,因为scala 2.10.RC1已经用完(20/10/2012).您可以查看此scala 2.11的SIP,它指出模式匹配器中的插值字符串将是有效的语法.使用新的字符串插值,您可以执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
Ben*_*mes 22
它不在(已发布的)scala库中.但是有一个SIP(Scala改进过程)用于添加此功能:
http://docs.scala-lang.org/sips/pending/string-interpolation.html
Lui*_*hys 14
你可以做C风格:
"Interpolate my %s here" format List(1,2,3)
//String = Interpolate my List(1, 2, 3) here
Run Code Online (Sandbox Code Playgroud)
要么
List(1,2,3) formatted "Interpolate my %s here"
Run Code Online (Sandbox Code Playgroud)
您可以在任何带有toString的东西上使用它们(即任何东西)
case class Foo(n: Int)
Foo(42) formatted "Here is a %s !!!!"
//String = Here is a Foo(42) !!!!
Run Code Online (Sandbox Code Playgroud)
虽然前者在单个字符串中启用多个插值方面更灵活(因为它可以采用多个参数).
我在scala 2.9上使用xml hack
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5428 次 |
最近记录: |