How to Split a String on newlines in C#?

Published on October 15, 2013 by Senthil Kumar

Filed under C#

Last modified October 15, 2013

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 15631 times

Ever wanted to split an input string in to new lines in .NET (c#) that has a new line character? Below is a sample code snippet that demonstrates how to do it.

How to Split a String on newlines in C#?

using System;

using System.Collections.Generic;

using System.Data;

using System.DirectoryServices.AccountManagement;

using System.IO;

using System.Linq;

using System.Net;

using System.Net.Sockets;

namespace AbundantCode


internal class Program


// How to Split a String on newlines in C# ?

private static void Main(string[] args)


string input = " \n is a \n Programming website";

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

foreach(var item in AllInputs)






How to Split a String on newlines in C#?


  1. You can do in this way also….

    string test = “One\nTwo\r\nThree\nFour\n”;
    string[] result = test.Split(new string[] { “\n”, “\r\n” }, StringSplitOptions.RemoveEmptyEntries);


    July 7, 2014 - 2:22 pm – Reply

  2. Separates strings. Often strings have delimiter characters in their data. Delimiters include “\r\n” newline sequences and the comma and tab characters. Split handles splitting upon string and character delimiters.

    ASP.Net C# Split string function provides the functionality to split the string into a string array by specifying its delimiters. C# split string function splits the string into array collection according to the number of separators passed to the split function. ASP.Net C# split string function removes the delimiters from the string and stores each part separated at consecutive indexes of array object. In ASP.Net 2.0 C# split string function has 6 overloads as follows.
    for full implementation refer here:

    December 3, 2014 - 12:34 pm – Reply

Leave a Comment


You might be Interested in these Posts

C Program to convert lower case string to upper case

Problem Statement Write a program in C to convert the given string from lower case to upper case. ...

How to decode a base64 string in C#?

Here's a sample code snippet demonstrating how you can easily decode a base64 encoded string in C#. ...

How to disable editing of items in a combo box in c#?

When working in Win forms using C# and especially when you use the combo box , you would have notice...