How to Split a String on newlines in C#?

Published on October 15, 2013 by abundantcode

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 6511 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 = "ABundantcode.com \n is a \n Programming website";

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

foreach(var item in AllInputs)

Console.WriteLine(item);

Console.ReadKey();

}

}

}

How to Split a String on newlines in C#?

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

2 Comments

  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);

    zene

    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: http://www.mindstick.com/Articles/996c0a4b-f9c0-4381-85a0-41da6d0c7f63/String%20Split%20in%20C

    December 3, 2014 - 12:34 pm – Reply

Leave a Comment


x

You might be Interested in these Posts

C# and Lambda - Finding all the elements of an integer array less than 35

Here's a sample code snippet demonstrating how to find all the elements of an integer array that is ...

C# and LINQ - Finding all the elements of an integer array less than 35

Here's a sample code snippet demonstrating how to find all the elements of an integer array that is ...

How to get the CommandLine of the Current Application in C# ?

You might need to get the information that contains the commandline which is used to execute the cur...