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!!!!!)
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.
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.
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.
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.
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:
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.
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:
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 $ {}.
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
You don't need to escape quotes in triple-quoted string, so s""""$value"""""
will work. Admittedly, it doesn't look good either.
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.
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"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With