Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

intellij idea - 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!!!!!)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a bug in Scala:

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
over two lines")
res1: java.lang.String = this is a string
over two lines

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...