Reputation: 69
Hi I have seen many examples for splitting the lines but how can I split lines in txt file if some lines contain \n in their string.
Suppose I have a file with following lines:
"Test \n Line1"
"Test Line 2"
How can I split these two lines as :
ResultArray = ['Test \n Line1', 'Test Line 2']
Upvotes: 1
Views: 3091
Reputation: 1744
You can also try this one,
In your text file just put your strings each in newline without that quotes. After that, your text file should be look something like this,
Test \n Line1
Test Line 2
And where you are reading your text file write
var ResultArray;
fs.readFile('test', 'utf8', function(err, contents) {
ResultArray = contents.split("\n");
});
Now your resultArray will look like
ResultArray = ['Test \\n Line1', 'Test Line 2' ];
Don't worry that your resultArray
contains items with double \\n
. When you try to get some value from your array it will have only one \
like when you do console.log(resultArray[0])
your output will be like
Test \n Line1
Upvotes: 2