Search code examples
stringscalainputsplitline-breaks

Scala generic way to split on line breaks in string


I'm looking for a clean way to split on line breaks in a string in Scala, regardless of if the line break is CR and/or LF. I've been using .split with a basic regex, but it seems ugly and strikes me as something a standard library would naturally encapsulate for system-agnostic code-- is there a standard library function for this?

Thanks in advance!

edit:

I like my code to be system-agnostic, as that one of the great selling points of the JVM. Having my code be aware of different OS conventions in a hardcoded string-literal regex seems to (very slightly) break that. It seems like something that should naturally be abstracted by the feature-rich Scala standard library, and I was just curious to see if a built-in method existed.


Solution

  • scala.io.Source.fromString(string).getLines.toList perhaps?