Scala Raw Strings: Extra tabs at the start of each line
I'm using开发者_开发百科 a raw strings but when I print the string I'm getting extra tabs at the start of each line.
val rawString = """here is some text
and now im on the next line
and this is the thrid line, and we're done"""
println(rawString)
this outputs
here is some text
and now im on the next line
and this is the thrid line, and we're done
I've tried setting different line endings but that had no effect. I'm working on the Mac (OS X tiger) using jEdit as my editor. I get the same result when I run the script in the scala interpreter or when I write the output to file.
Anybody know whats going on here?
This problem is due to the fact that you are using multiline raw strings in the interpreter. You can see that the width of the extra spaces is exactly the size of the scala>
prompt or the size of the pipe + spaces added by the interpreter when you create a new line in order to keep things lined up.
scala> val rawString = """here is some text // new line
| and now im on the next line // scala adds spaces
| and this is the thrid line, and we're done""" // to line things up
// note that the comments would be included in a raw string...
// they are here just to explain what happens
rawString: java.lang.String =
here is some text
and now im on the next line
and this is the thrid line, and we're done
// you can see that the string produced by the interpreter
// is aligned with the previous spacing, but without the pipe
If you write your code in a Scala script and run it as scala filename.scala
you do not get the extra tabs.
Alternatively, you can use the following construct in the interpreter:
val rawString = """|here is some text
|and now im on the next line
|and this is the thrid line, and we're done""".stripMargin
println(rawString)
What stripMargin
does is strips anything before and including the |
character at the start of every line in the raw string.
EDIT: This is a known Scala bug -- thanks extempore :)
UPDATE: Fixed in trunk. Thanks extempore again :).
Hope it helps :)
-- Flaviu Cipcigan
精彩评论