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 9587 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#?

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

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...

C# Program to find the ASCII Value of a Character

Problem Write a program in Visual C# to find the ASCII value of the input character. How to find t...