Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and filtering a file into a string

Tags:

scala

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?

like image 267
WannaKnow Avatar asked Feb 17 '23 11:02

WannaKnow


1 Answers

Add some parameters to mkString:

val fileAsFilteredString = io.Source.fromFile("file.txt").getLines
    .filter(s => !(s contains "filter these")).mkString("\n")
like image 53
Noah Avatar answered Feb 27 '23 15:02

Noah