I am new to Scala and I need to read the contents of a text file into a string while removing certain lines at the same time. The lines to be removed can be identified with a substring match. I could come up with the following solution, which almost works, the only problem is that the newlines are removed:
val fileAsFilteredString = io.Source.fromFile("file.txt").getLines.filter(s => !(s contains "filter these")).mkString;
How can I keep the newlines?
Add some parameters to mkString
:
val fileAsFilteredString = io.Source.fromFile("file.txt").getLines
.filter(s => !(s contains "filter these")).mkString("\n")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With