1

I have a 10 million line text file loaded stored as a string in a variable "someString". I want to generate an array where each line is an item. I know that to do that I would do the following:

var files = someString.split("\n");

However, this keeps the line break symbol \n after each item. What is the most efficient way to trim this from each item?

ideasandstuff
  • 47
  • 1
  • 7
  • 2
    Chances are the data is in `CRLF` format (typical for Windows text). You're splitting on `LF` (`\n`) but the `CR` (`\r`) is still present – Phil Dec 29 '16 at 02:39
  • 1
    10M lines is a lot of data to handle in JS. You might be better off with something that can stream the data instead of loading it all into memory – Phil Dec 29 '16 at 02:48

2 Answers2

2

Try using a regex delimiter to handle different line-ending characters for Windows (\r\n) and others (\n or \r)

someString.split(/\r\n|\r|\n/)
Phil
  • 157,677
  • 23
  • 242
  • 245
-1

I found this answer on How to remove all line breaks from a string?.

someText = someText.replace(/(\r\n|\n|\r)/gm,"");

Community
  • 1
  • 1
  • 1
    OP wants an array of strings, not a single string with all end-of-line characters removed – Phil Dec 29 '16 at 02:46