How to insert double quotes into String with interpolation in scala

后端 未结 11 1575
北荒
北荒 2020-12-05 12:53

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)

相关标签:
11条回答
  • 2020-12-05 13:00

    You don't need to escape quotes in triple-quoted string, so s""""$value""""" will work. Admittedly, it doesn't look good either.

    0 讨论(0)
  • 2020-12-05 13:01

    For your use case, they make it easy to achieve nice syntax.

    scala> implicit class `string quoter`(val sc: StringContext) {
         | def q(args: Any*): String = "\"" + sc.s(args: _*) + "\""
         | }
    defined class string$u0020quoter
    
    scala> q"hello,${" "*8}world"
    res0: String = "hello,        world"
    
    scala> "hello, world"
    res1: String = hello, world       // REPL doesn't add the quotes, sanity check
    
    scala> " hello, world "
    res2: String = " hello, world "   // unless the string is untrimmed
    

    Squirrel the implicit away in a package object somewhere.

    You can name the interpolator something besides q, of course.

    Last week, someone asked on the ML for the ability to use backquoted identifiers. Right now you can do res3 but not res4:

    scala> val `"` = "\""
    ": String = "
    
    scala> s"${`"`}"
    res3: String = "
    
    scala> s"hello, so-called $`"`world$`"`"
    res4: String = hello, so-called "world"
    

    Another idea that just occurred to me was that the f-interpolator already does some work to massage your string. For instance, it has to handle "%n" intelligently. It could, at the same time, handle an additional escape "%q" which it would not pass through to the underlying formatter.

    That would look like:

    scala> f"%qhello, world%q"
    <console>:9: error: conversions must follow a splice; use %% for literal %, %n for newline
    

    That's worth an enhancement request.

    Update: just noticed that octals aren't deprecated in interpolations yet:

    scala> s"\42hello, world\42"
    res12: String = "hello, world"
    
    0 讨论(0)
  • 2020-12-05 13:09

    As already mentioned, this is a known bug in Scala. A workaround is to use \042.

    0 讨论(0)
  • 2020-12-05 13:14

    Simple way:-

    val str="abc"
    println(s"$str") //without double quotes
    println(s"""\"$str\"""") // with double quotes
    
    0 讨论(0)
  • 2020-12-05 13:16

    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}"
     }
    
    0 讨论(0)
  • 2020-12-05 13:17

    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

    0 讨论(0)
提交回复
热议问题