How to insert double quotes into String with inter

2020-01-30 23:54发布

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

11条回答
来,给爷笑一个
2楼-- · 2020-01-31 00:33

This fixed the problem for me, I tested this out and this is what I used.

raw""" 
   Inside this block you can put "as many" quotes as you "want" and even "${5 + 7}" interpolate inside the quotes 
"""

http://docs.scala-lang.org/overviews/core/string-interpolation.html#the-raw-interpolator

查看更多
看我几分像从前
3楼-- · 2020-01-31 00:33

Simple way:-

val str="abc"
println(s"$str") //without double quotes
println(s"""\"$str\"""") // with double quotes
查看更多
走好不送
4楼-- · 2020-01-31 00:34

Taking @Pascalius suggestion a few steps further. class StringImprovements extends and inherits AnyVal.

object StringUtil{
    implicit class StringImprovements(val s: String) extends AnyVal {
        def dqt = "\""+s+"\""    // double quote
        def sqt = s"'$s'"        // single quote
    }
}

Scala only uses the StringImprovements class to create an intermediate object on which to call implicitly the two extension methods dqt & sqt. Nevertheless, we can eliminate the creation of this object and improve performance by making the class inherit from AnyVal. For Scala provides the value type specifically for such cases where the compiler will replace the object by just making the call to the method directly.

Here is a simple example using the above implicit class in an intermix where we use named variables (string & boolean) and a function in the interpolation string.

import StringUtil._
abstract class Animal {
   ...
   override def toString(): String = s"Animal:${getFullName().dqt}, CanFly:$canFly, Sound:${getSound.dqt}"
 }
查看更多
家丑人穷心不美
5楼-- · 2020-01-31 00:35

How about

s"This is ${"\"" + variable + "\""}" inserted in string with quotes

查看更多
家丑人穷心不美
6楼-- · 2020-01-31 00:35

It's heavily used in my case, therefore I created this version:

object StringUtil{
    implicit class StringImprovements(s: String) {
        def quoted = "\""+s+"\""
    }
}

val myStatement = s"INSERT INTO ${tableName.quoted} ..."
查看更多
三岁会撩人
7楼-- · 2020-01-31 00:43

Another solution (also mentioned in the Scala tracker) is to use

case _ => s"${'"'}$value${'"'}"

Still ugly, but sometimes perhaps may be preferred over triple quotes.

It seems an escape sequence $" was suggested as a part of SIP-24 for 2.12:

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

This SIP was never accepted, as it contained other more controversial suggestions. Currently there is an effort to get escape sequence $" implemented in 2.13 as Pre SIP/mini SIP $” escapes in interpolations.

查看更多
登录 后发表回答