This does not work:
scala> """one\r\ntwo\r\nthree\r\nfour""".replace("\r\n", "\n")
res1: String = one\r\ntwo\r\nthree\r\nfour
How to do that in Scala?
Is there a more idiomatic way of doing that, instead of using replace?
This does not work:
scala> """one\r\ntwo\r\nthree\r\nfour""".replace("\r\n", "\n")
res1: String = one\r\ntwo\r\nthree\r\nfour
How to do that in Scala?
Is there a more idiomatic way of doing that, instead of using replace?
The problem is that """
quotes does not expand escape sequences. Three different approaches:
"
quotes in order to treat escape sequences correctly: "one\r\ntwo"
;s
string interpolator, be careful following this approach cause this could lead to unexpected replacements: s"""one\r\ntwo"""
;treatEscapes
directly to expands escape sequences in your string: StringContext.treatEscapes("""one\r\ntwo""")
.Refer also to this earlier question.
try this
"""one\r\ntwo\r\nthree\r\nfour""".replace("\\r\\n", "\n")
\
is treated as escape
charater within string, so you need to tell the compiler
that its not a escape
character but a string
.