String Scala原始字符串:每行开头的额外选项卡

String Scala原始字符串:每行开头的额外选项卡,string,scala,String,Scala,我使用的是原始字符串,但当我打印字符串时,我会在每行的开始处得到额外的制表符 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) 这个输出 here is some text and now im on the next line and this is the thri

我使用的是原始字符串,但当我打印字符串时,我会在每行的开始处得到额外的制表符

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)
这个输出

here is some text
    and now im on the next line
    and this is the thrid line, and we're done
我试过设置不同的行尾,但没有效果。 我正在使用jEdit作为我的编辑器开发Mac(OS X tiger)。在scala解释器中运行脚本或将输出写入文件时,会得到相同的结果


有人知道这里发生了什么吗?

这个问题是由于您在解释器中使用了多行原始字符串。您可以看到,额外空格的宽度正好是
scala>
提示符的大小,或者是创建新行时解释器添加的管道+空格的大小,以便保持对齐

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
如果在Scala脚本中编写代码,并将其作为
Scala filename.Scala
运行,则不会获得额外的选项卡

或者,您可以在解释器中使用以下构造:

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)
stripMargin
所做的是在原始字符串的每一行开始处去除前面的任何内容,并包括
|
字符

编辑:这是一个--谢谢:)

更新:已修复。再次感谢:)

希望有帮助:)


--Flaviu Cipcigan

多行XML也会出现这种情况。现在我想起来了。。。我认为这是一个错误。REPL没有理由添加我自己没有写的东西。是的,Flaviu对Scala问题的回答非常好,我现在很少回答问题。@Brian:不客气:)@Daniel:我还以为这是个bug,但是它似乎仍然存在于最近的2.8夜间版本中,并且标准库中以
stripMargin
的形式构建了一个变通方法(尽管
stripMargin
在其他情况下很有用…)您知道关于这个问题有什么bug报告吗?@Daniel:谢谢大家的支持!Scala SO社区实际上是我尽可能接近真实世界Scala体验的方式。我正在为我的计算物理项目认真考虑Scala,但在我能说服合适的人之前,我现在做的大部分Scala都是如此。虽然不担心。。。从周一开始上大学可能会影响我的社交能力:)。我肯定需要一位智者来回答更多的scala问题;)