Scala 中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6193960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New line character in Scala
提问by Luigi Plinge
Is there a shorthand for a new line character in Scala? In Java (on Windows) I usually just use "\n", but that doesn't seem to work in Scala - specifically
Scala 中是否有换行符的简写?在 Java(在 Windows 上)中,我通常只使用“\n”,但这在 Scala 中似乎不起作用 - 特别是
val s = """abcd
efg"""
val s2 = s.replace("\n", "")
println(s2)
outputs
输出
abcd
efg
in Eclipse,
在 Eclipse 中,
efgd
(sic) from the command line, and
(原文如此)从命令行,和
abcdefg
from the REPL (GREAT SUCCESS!)
来自 REPL(非常成功!)
String.format("%n")works, but is there anything shorter?
String.format("%n")有效,但有什么更短的吗?
采纳答案by James Iry
Your Eclipse making the newline marker the standard Windows \r\n, so you've got "abcd\r\nefg". The regex is turning it into "abcd\refg" and Eclipse console is treaing the \r slightly differently from how the windows shell does. The REPL is just using \n as the new line marker so it works as expected.
您的 Eclipse 使换行符成为标准的 Windows \r\n,所以您有“abcd\r\nefg”。正则表达式将其转换为“abcd\refg”,Eclipse 控制台处理 \r 的方式与 Windows shell 的处理方式略有不同。REPL 只是使用 \n 作为新行标记,所以它按预期工作。
Solution 1: change Eclipse to just use \n newlines.
解决方案 1:将 Eclipse 更改为仅使用 \n 换行符。
Solution 2: don't use triple quoted strings when you need to control newlines, use single quotes and explicit \n characters.
解决方案 2:当需要控制换行符时不要使用三引号字符串,使用单引号和显式的 \n 字符。
Solution 3: use a more sophisticated regex to replace \r\n, \n, or \r
解决方案 3:使用更复杂的正则表达式来替换 \r\n、\n 或 \r
回答by Ed Staub
A platform-specific line separator is returned by
平台特定的行分隔符由
sys.props("line.separator")
This will give you either "\n" or "\r\n", depending on your platform. You can wrap that in a val as terse as you please, but of course you can't embed it in a string literal.
这将为您提供“\n”或“\r\n”,具体取决于您的平台。您可以将它尽可能简洁地包装在 val 中,但当然您不能将其嵌入字符串文字中。
If you're reading text that's not following the rules for your platform, this obviously won't help.
如果您正在阅读不符合您平台规则的文本,这显然无济于事。
References:
参考:
- scala.sys package scaladoc(for sys.props)
- java.lang.System.getProperties javadoc(for "line.separator")
- scala.sys 包 scaladoc(用于 sys.props)
- java.lang.System.getProperties javadoc(用于“line.separator”)
回答by Dmitry Ginzburg
If you're sure the file's line separator in the one, used in this OS, you should do the following:
如果您确定文件的行分隔符在此操作系统中使用,则应执行以下操作:
s.replaceAll(System.lineSeparator, "")
Elsewhere your regex should detect the following newline sequences: "\n" (Linux), "\r" (Mac), "\r\n" (Windows):
在其他地方,您的正则表达式应检测以下换行符序列:“\n”(Linux)、“\r”(Mac)、“\r\n”(Windows):
s.replaceAll("(\r\n)|\r|\n", "")
The second one is shorter and, I think, is more correct.
第二个更短,我认为更正确。
回答by skryty
Try this interesting construction :)
试试这个有趣的结构:)
import scala.compat.Platform.EOL
println("aaa"+EOL+"bbb")
回答by Keith Pinson
var s = """abcd
efg""".stripMargin.replaceAll("[\n\r]","")
回答by OscarRyz
Use \r\ninstead
使用\r\n替代
Before:
前:


After:
后:



