0

Here it is printed without line break, OK

scala> println(raw"Hello \n World")
Hello \n World

But, how is line break working here on storing in a value and also even after using raw

scala> val word = "Hello \n World"
scala> println(raw"$word")
Hello 
 World
supernatural
  • 796
  • 2
  • 11
  • Does this answer your question? [Scala convert a string to raw](https://stackoverflow.com/questions/25109578/scala-convert-a-string-to-raw) – joel Dec 14 '19 at 11:32

2 Answers2

3

To clarify on @Joel Berkeley's answer:

When using the raw interpolator, as in raw"\n", you get a String which has two charaters in it, verbatim, a \ and a n.

Now, when you have a normal String, e.g. "\n", you get a String with a single character, namely a "line feed" LF, ASCII code 10.

Then, if you embed that into a new string, using whatever interpolator you want (ie. it does not matter whether you use s, f, or raw), you still get the same character no matter what since the value of the string is inserted in the resulting string.

Just to reiterate, there is no "raw format", you always just get a String, it's just a different interpretation of the character of the string itself when it is constructed.

cbley
  • 4,133
  • 1
  • 14
  • 29
1

I'd guess the string is defined as raw at compile time not runtime, so you'd need to

val word = raw"Hello \n World"
println(s"$word")

See also this post, which doesn't appear to cover this.

joel
  • 3,282
  • 1
  • 19
  • 38