New line character in Scala

后端 未结 6 1926
既然无缘
既然无缘 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:40

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

    s.replaceAll("(\r\n)|\r|\n", "")
    

    The second one is shorter and, I think, is more correct.

提交回复
热议问题