Nimrod007 Nimrod007 - 1 year ago 108
Scala Question

How to insert double quotes into String with interpolation in scala

Having trouble escaping all the quotes in my function

(basic usage of it -> if i find a string do nothing, if its not a string add " in the begin and end)

code snippet :

def putTheDoubleQuotes(value: Any): Any = {
value match {
case s: String => s //do something ...
case _ => s"\"$value\"" //not working

only thing that worked was :

case _ => s"""\"$value\""""

is there a better syntax for this ?

it looks terrible and the IDE (IntelliJ) marks it in red (but lets you run it which really pisses me!!!!!)

Answer Source

this is a reported bug in Scala and IntelliJ

escape does not work with string interpolation

but maybe you can use:

scala> import org.apache.commons.lang.StringEscapeUtils.escapeJava
import org.apache.commons.lang.StringEscapeUtils.escapeJava

scala> escapeJava("this is a string\nover two lines")
res1: java.lang.String = this is a string\nover two lines
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download