Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert double quotes into String with interpolation in scala

Tags:

string

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

like image 362
Nimrod007 Avatar asked Jan 13 '14 07:01

Nimrod007


People also ask

How do you add double quotes to a string variable?

To place quotation marks in a string in your code In Visual Basic, insert two quotation marks in a row as an embedded quotation mark. In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark.

How do you pass a string with double quotes?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

What is string interpolation in Scala?

String Interpolation refers to substitution of defined variables or expressions in a given String with respected values. String Interpolation provides an easy way to process String literals. To apply this feature of Scala, we must follow few rules: String must be defined with starting character as s / f /raw.

How do you string multiple quotes together?

Optional Rule # 4. Even if a paragraph lists quotes from more than one source, you can still summarize them into one reference placed after the last quote or at the end of the paragraph. In this case, separate the different authors listed in your reference by a semicolon, in both MLA and Chicago.

How do I interpolate a string in Scala?

Scala provides three string interpolation methods out of the box: s, f and raw. Prepending s to any string literal allows the usage of variables directly in the string. You’ve already seen an example here:

How to insert double quotes into a string in Java?

Insert double quotes into a string in Java You can achieve it by following ways:- 1) By Escape characters An escape character is a two-character sequence that starts with \ also, it represents a special character, that would otherwise be printable.

How do I create a multiline string in Scala?

In Scala you create multiline strings by surrounding your text with three double quotes: Although this solution works, the second and third lines in this example will end up with whitespace at the beginning of their lines. If you print the string, it looks like this:

How do you use interpolation in Python?

With the s interpolator, any name that is in scope can be used within a string. String interpolators can also take arbitrary expressions. For example: will print the string 1 + 1 = 2. Any arbitrary expression can be embedded in $ {}.


4 Answers

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\nover two lines") res1: java.lang.String = this is a string\nover two lines 
like image 90
abzcoding Avatar answered Sep 16 '22 17:09

abzcoding


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

like image 45
Alexey Romanov Avatar answered Sep 18 '22 17:09

Alexey Romanov


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.

like image 29
Suma Avatar answered Sep 17 '22 17:09

Suma


An example:

scala> val username="admin"
> username: String = admin

scala> val pass="xyz"
> pass: String = xyz

scala> println(s"""{"username":"$username", "pass":"$pass"}""")
> {"username":"admin", "pass":"xyz"}
like image 30
R Sun Avatar answered Sep 20 '22 17:09

R Sun