Coder Perfect

In.NET, what is the simplest way to split a string on newlines?

Problem

In.NET, I need to split a string into newlines, and the Split function is the only way I know of to do so. However, I won’t be able to (easily) divide on a newline if I do it this way, so what’s the best way to accomplish it?

Asked by RCIX

Solution #1

You must use the overload that takes an array of strings to split on a string:

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

Edit: You can leverage the ability to match multiple strings to handle different forms of line breaks in a text. This will split the text correctly on either sort of line break and keep the text’s empty lines and spacing:

string[] lines = theText.Split(
    new string[] { "\r\n", "\r", "\n" },
    StringSplitOptions.None
);

Answered by Guffa

Solution #2

Why not use a StringReader instead?

using (System.IO.StringReader reader = new System.IO.StringReader(input)) {
    string line = reader.ReadLine();
}

Answered by Clément

Solution #3

You should be able to simply split your string like this:

aString.Split(Environment.NewLine.ToCharArray());

Answered by nikmd23

Solution #4

Avoid using string whenever possible. Split is a good choice for a broad solution because it uses extra memory everywhere it’s used: the original string and the split copy are both stored in memory. When it comes to scaling, believe me when I say that a 32-bit batch-processing app processing 100MB documents will max out at eight concurrent threads. Not that I’ve ever been there…

Use an iterator like this instead.

public static IEnumerable<string> SplitToLines(this string input)
{
    if (input == null)
    {
        yield break;
    }

    using (System.IO.StringReader reader = new System.IO.StringReader(input))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            yield return line;
        }
    }
}

This will enable you to run a memory-saving loop over your data.

foreach(var line in document.SplitToLines()) 
{
    // one line at a time...
}

Of course, you can do this if you want to remember everything;

var allTheLines = document.SplitToLines().ToArray();

Answered by Steve Cooper

Solution #5

Use the following in an extension class based on Guffa’s response:

public static string[] Lines(this string source) {
    return source.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
}

Answered by Erwin Mayer

Post is based on https://stackoverflow.com/questions/1547476/easiest-way-to-split-a-string-on-newlines-in-net