Easiest way to split a string on newlines in .NET? – Dev

The best answers to the question “Easiest way to split a string on newlines in .NET?” in the category Dev.

QUESTION:

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?

ANSWER:

What about using a StringReader?

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

ANSWER:

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", "\r", "\n" },
    StringSplitOptions.None
);

ANSWER:

Try to avoid using string.Split for a general solution, because you’ll use more memory everywhere you use the function — the original string, and the split copy, both in memory. Trust me that this can be one hell of a problem when you start to scale — run a 32-bit batch-processing app processing 100MB documents, and you’ll crap out at eight concurrent threads. Not that I’ve been there before…

Instead, use an iterator like this;

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 allow you to do a more memory efficient loop around your data;

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

Of course, if you want it all in memory, you can do this;

var allTheLines = document.SplitToLines.ToArray();

ANSWER:

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

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