Reading and filtering a file into a string

I am new to Scala and I need to read the contents of a text file in a line while deleting certain lines. The lines to be deleted can be identified using a substring. I could find the following solution that almost works, the only problem is that new lines are deleted:

val fileAsFilteredString = io.Source.fromFile("file.txt").getLines.filter(s => !(s contains "filter these")).mkString; 

How to save newlines?

+4
source share
1 answer

Add some parameters to mkString :

 val fileAsFilteredString = io.Source.fromFile("file.txt").getLines .filter(s => !(s contains "filter these")).mkString("\n") 
+8
source

Source: https://habr.com/ru/post/1482002/


All Articles