我正在使用原始字符串但是当我打印字符串时,我会在每行的开头获得额外的标签.
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)
Run Code Online (Sandbox Code Playgroud)
这个输出
here is some text
and now im on the next line
and this is the thrid line, and we're done
Run Code Online (Sandbox Code Playgroud)
我尝试过设置不同的行结尾,但没有效果.我正在使用jEdit作为我的编辑器在Mac(OS X tiger)上工作.当我在scala解释器中运行脚本或将输出写入文件时,我得到相同的结果.
有谁知道这里发生了什么?
Fla*_*gan 11
此问题是由于您在解释器中使用多行原始字符串.您可以看到额外空格的宽度正好是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
Run Code Online (Sandbox Code Playgroud)
如果您在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)
Run Code Online (Sandbox Code Playgroud)
什么stripMargin是|在原始字符串中每行开头之前包括任何内容的字符.
编辑:这是一个已知的Scala错误 - 感谢临时工 :)
希望能帮助到你 :)
- Flaviu Cipcigan