Quick C# code snippet: split a string into lines

Posted: (EET/GMT+2)

 

I find myself pretty often in a situation where I need to split a C# string into lines that are delimited with the regular Windows carriage return (CR) and line feed pairs, CRLF in ASCII parlance.

Well, the C# string class contains the Split method, but I always wonder why the parameter overloads do not allow you to give a single string parameter. Thus, I'm sharing my code snippet that I'm pretty often using in a situation like this.

Here goes:

internal static string[] SplitToLines(this string input)
{
  return input.Split(new string[] { "\r\n" },
    StringSplitOptions.RemoveEmptyEntries);
}

Happy hacking!