New line character in Scala

后端 未结 6 1975
既然无缘
既然无缘 2021-01-01 13:08

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



        
6条回答
  •  一个人的身影
    2021-01-01 13:30

    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.

    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")

提交回复
热议问题