views:

1122

answers:

5

I need to split a string into newlines in .NET and the only way i know of to split strings is with the Split method. However that will not allow me to (easily) split on a newline, so what is the best way to do it?

+10  A: 

To split on a string you need to use the overload that takes an array of strings:

string[] lines = theText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

Edit:
If you want to handle different types of line breaks in a text, you can use the ability to match more than one string. This will correctly split on either type of line break, and preserve empty lines and spacing in the text:

string[] lines = theText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
Guffa
I suspected as much. Also, you need another StringSplitOptions argument in there. I was just hoping there was a less clunky way of doing it.
RCIX
Move the clunk into your own method - possibly in your own StringUtils class.
slim
@RCIX: Sending the correct parameters to the method is a bit awkward because you are using it for something that is a lot simpler than what it's capable of. At least it's there, prior to framework 2 you had to use a regular expression or build your own splitting routine to split on a string...
Guffa
+1 I like your edited answer better than the accepted one.
Maslow
+6  A: 

You should be able to split your string pretty easily, like so:

aString.Split(Environment.NewLine.ToCharArray());
nikmd23
On a non-*nix system that will split on the separate characters in the Newline string, i.e. the CR and LF characters. That will cause an extra empty string between each line.
Guffa
Correct me if i'm wrong, but won't that split on the characters \ and n?
RCIX
@RCIX: No, the \r and \n codes represent single characters. The string "\r\n" is two characters, not four.
Guffa
Ok i'll go with this then. Thanks!
RCIX
Don't go with this, due to Guffa's reasoning.
slim
So should we use this or not?
IainMH
@IainMH: No, you shouldn't. As I explained it will return an extra empty line between each line.
Guffa
I wouldn't catch that in my case, but i'll switch back immediately.
RCIX
Why is this Accepted? It's broken beyond repair.
bzlm
if you add the parameter StringSplitOptions.RemoveEmptyEntries, then this will work perfectly.
Ruben
@Ruben: No, it will not. Serge already suggested that in his answer, and I have aldready explained that it will also remove the empty lines in the original text that should be preserved.
Guffa
A: 

I did not know about Environment.Newline, but I guess this is a very good solution.

My try would have been:

        string str = "Test Me\r\nTest Me\nTest Me";
        var splitted = str.Split('\n').Select(s => s.Trim()).ToArray();

The additional .Trim removes any \r or \n that might be still present (e. g. when on windows but splitting a string with os x newline characters). Probably not the fastest method though.

EDIT:

As the comments correctly pointed out, this also removes any whitespace at the start of the line or before the new line feed. If you need to preserve that whitespace, use one of the other options.

Max
The Trim will also remove any white space at the beginning and end of lines, for example indentation.
Guffa
".Trim removes any \r or \n that might be still present" - ouch. Why not write robust code instead?
bzlm
Maybe I got the question wrong, but it was/is not clear of that whitespace must be preserved. Of course you are right, Trim() also removes whitespace.
Max
@Max: Wow, wait until I tell my boss that code is allowed to do anything that is not specifically ruled out in the specification... ;)
Guffa
@Guffa: Haha, you got me there. Thats a point for you. ;)
Max
+1  A: 
MaciekTalaska
The RemoveEmptyEntries option will remove empty lines from the text. That may be desirable in some situations, but a plain split should preserve the empty lines.
Guffa
yes, you're right, I just made this assumption, that... well, blank lines are not interesting ;)
MaciekTalaska
A: 
string[] lines = text.Split(
  Environment.NewLine.ToCharArray(), 
  StringSplitOptions.RemoveEmptyStrings);

The RemoveEmptyStrings option will make sure you don't have empty entries due to \n following a \r

(Edit to reflect comments:) Note that it will also discard genuine empty lines in the text. This is usually what I want but it might not be your requirement.

Serge - appTranslator
The RemoveEmptyStrings options will also remove empty lines, so it doesn't work properly if the text has empty lines in it.
Guffa
You probably want to preserve genuine empty lines : \r\n\r\n
slim