16

For a class project I have to load a text file into a linked list. So far, I have been able to read from the file, but I am struggling to split it up into sections so that I can put it into the linked list.

For example, I would like to split these items up at the empty lines:

David
Hunter
No1
Admin

John
Smith
No11
Sales

Jane
Appleby
No5
Accounts

I have tried String[] people = record.Split('\n'); but of course, this just splits it at every line.

I have also tried:
String[] people = record.Split('\n\r');
String[] people = record.Split('\r\n');
String[] people = record.Split('\n\n');
but it won't compile due to "too many characters in character literal"

Could anyone please suggest a way to do this (preferably without regex)?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
gcstudent
  • 171
  • 1
  • 1
  • 6
  • Please show us how you “read get the file”. – Dour High Arch Sep 07 '15 at 01:27
  • 3
    Split on `"\r\n\r\n"` - notice double quotes for string rather than single quotes for character. – Blorgbeard Sep 07 '15 at 01:27
  • Post the actual input of text file and also attach the code that extracts `record` variable. – Orel Eraki Sep 07 '15 at 01:27
  • 2
    `txt.Split(new string[] { Environment.NewLine + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)` – Sky Fang Sep 07 '15 at 01:31
  • This is not a duplicate of [split a string on newlines](http://stackoverflow.com/questions/1547476/easiest-way-to-split-a-string-on-newlines-in-net), because OP wants only to split on *empty* lines. – Blorgbeard Sep 07 '15 at 01:33
  • @SkyFang why Environment.NewLine + Environment.NewLine? Would have thought record.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) would suffice – Mick Sep 07 '15 at 01:35
  • @mick `Environment.NewLine` just one line,in windows it seems to be `\r\n`,so need to use `Environment.NewLine + Environment.NewLine` – Sky Fang Sep 07 '15 at 01:38
  • @SkyFang Environment.NewLine.ToCharArray() == { '\r', '\n' } && Environment.NewLine == "\r\n" so Environment.NewLine + Environment.NewLine wouldn't be correct – Mick Sep 07 '15 at 01:43
  • if anything other than Split(new string[] { Environment.NewLine }) you would want Split(new string[] { Environment.NewLine, "\r", "\n" }) – Mick Sep 07 '15 at 01:44
  • 1
    @Mick that would break on any "\r\n", "\r" or "\n". The OP wants to break on an empty line, i.e. two consecutive newlines ("\r\n\r\n"). – Fernando Matsumoto Sep 07 '15 at 04:20
  • @SkyFang's solution was the only one I could get to compile, but I really appreciate everyone's help and suggestions. Thanks guys! – gcstudent Sep 07 '15 at 06:25

1 Answers1

31

You can get it accomplished by using

string[] people = record.Split(new string[] { "\r\n\r\n" },
                               StringSplitOptions.RemoveEmptyEntries);

or

string[] people = record.Split(new string[] { Environment.NewLine + Environment.NewLine },
                               StringSplitOptions.RemoveEmptyEntries);

What it does is it removes empty entries with StringSplitOptions.RemoveEmptyEntries and then splits where two linebreaks are right after each other.

Felix D.
  • 4,811
  • 8
  • 38
  • 72
Mohit S
  • 13,723
  • 6
  • 34
  • 69