0

I have a textfile with lines that end with \r\n. When splitting 1 line with eg. Regex:

string[] splittedFile = Regex.Split(fileString, "\n");

Original input:

:020000040008F2\r\n

:04200000004875F02F\r\n

it will output:

:020000040008F2\r

:04200000004875F02F\r

However, I want it to be:

:020000040008F2\r\n

:04200000004875F02F\r\n

How can this be done?

Thanks!

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
Ruudster
  • 157
  • 1
  • 1
  • 6

3 Answers3

1

You could try the following:

string[] splittedFile = fileString.Split(
    new[] {Environment.NewLine},
    StringSplitOptions.None
);
MindSwipe
  • 7,193
  • 24
  • 47
Ranjith.V
  • 316
  • 1
  • 7
1

You can use positive look behind:

string[] splittedFile = Regex.Split(fileString, "(?<=\r\n)");
Andrew Arthur
  • 1,563
  • 2
  • 11
  • 17
0

How about you read the file as follows:

string[] lines = File.ReadAllLines("afile.txt");

lines will not include the new line characters at all in this case, but as you don't say what you want to do next, its hard to know whether you want them there or not. Say you were to want to write the lines out again to some other file, you'd just use WriteLine from eg FileStream and the endlines would be of no concern

Tim Rutter
  • 4,549
  • 3
  • 23
  • 47