0

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?

blueFast
  • 41,341
  • 63
  • 198
  • 344

2 Answers2

0

The problem is that """ quotes does not expand escape sequences. Three different approaches:

  1. Use single " quotes in order to treat escape sequences correctly: "one\r\ntwo";
  2. Use the s string interpolator, be careful following this approach cause this could lead to unexpected replacements: s"""one\r\ntwo""";
  3. Call treatEscapes directly to expands escape sequences in your string: StringContext.treatEscapes("""one\r\ntwo""").

Refer also to this earlier question.

Federico Pellegatta
  • 3,977
  • 1
  • 17
  • 29
0

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.

Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97