Reputation: 12712
How to replace the double quote by \":
val s = """I am "groot"."""
so the output will be """I am \"groot\"."""
I tried with but no luck
s.replaceAll('"', '\"')
Upvotes: 0
Views: 421
Reputation: 70347
So @Tanjin provides the correct solution. However, the reason your solution does not work is this.
s.replaceAll('"', '\"')
Backslashes have special meaning in string and character literals, so '\"'
compiles down to just the quote character. Running in the REPL will show you this
scala> '\"'
res2: Char = "
Meanwhile, using triple-quote strings disables this behavior.
scala> """\""""
res3: String = \"
Upvotes: 2