2017-01-02 31 views
2

刚刚起步斯卡拉,并有一个问题。如何在Scala中将raw与字符串插值结合起来?

val num = 10 
val str = "Learning\t${num}Scala" 

现在我试图打印str没有逃脱\tnum插值。这可能吗?尝试下面几个变化,但他们没有工作

scala>s"${str}" 
scala>s"""${str}""" 
scala>raw"""${str}""" 

的问题是如何打印Learning\t10Scala

+1

不知道我理解这个问题 - 为什么不单曲“学习\ T $ {NUM}斯卡拉”' ?您不能“延迟”插值,它只能在创建字符串时完成。 –

+0

@TzachZohar您的示例将用标签替换'\ t' – Bala

+1

''“”Learning \ t“”“+ s”$ {num} Scala“' – mike

回答

0

下面是一些可以做的,用同量的代码。

写函数调用次,并使其插入一些串的其他字符串中间

scala> def times(n: Int)(str: String): String = List.fill(n)(str).mkString("") 
times: (n: Int)String 

scala> s"""hello${times(3)("\t")}world""" 
res0: String = hello   world 
相关问题