Split a String on Newline in C#

Saad Aslam Jan 30, 2023 Mar 25, 2022
  1. Use String.Split() to Split a String on Newline in C#
  2. Use Regex.Split() to Split a String on Newline in C#
Split a String on Newline in C#

This article will introduce how we can split a string on newline using a specific character in C#.

There are two methods to split a string on newline, which work the same way. These are discussed and implemented in the following sections.

Use String.Split() to Split a String on Newline in C#

First, we need to import the System library to access all the methods and functions used in the example.

using System;

Then, let’s create a class SplitStringNLine, and within this class, we’ll make our Main() method.

public class SplitStringNLine
{
    public static void Main(string[] args) {}
}

We need a string that we can split on newline. So, let’s initialize a variable named stringToBeSplit and give it a random sentence that the Split() method will utilize.

string stringToBeSplit =
        "C Sharp " +
        "is " +
        "A " +
        "Programming " +
        "Language " +
        "available " +
        "globally.";

We’ll need a variable to keep the value of the split string after we’ve initialized the random string.

string[] splitString;

Then, we need to initialize this variable by assigning it the stringToBeSplit with the Split() function, passing a string r. As soon as it encounters an r character, it will split the string to a new line using the Split() method.

splitString = stringToBeSplit.Split("r");

Let us present the user with the original string.

Console.WriteLine("String to be Split");
Console.WriteLine(stringToBeSplit);

Now, we need to output the split string. We need to use a loop here because every time it splits the string, it needs to be shown in the console.

We’ll use a foreach loop that checks the string and displays it after each iteration, terminating automatically after the splitting process ends.

Console.WriteLine("\nSplit string by the *r* character:");
foreach (string str in splitString)
{
    Console.WriteLine(str);
}

Source code:

using System;

public class SplitStringNLine
{
    public static void Main(string[] args)
    {
        string stringToBeSplit =
                "C Sharp " +
                "is " +
                "A " +
                "Programming " +
                "Language " +
                "available " +
                "globally.";

        string[] splitString;
        splitString = stringToBeSplit.Split("r");
        Console.WriteLine("String to be Split");
        Console.WriteLine(stringToBeSplit);
        Console.WriteLine("\nSplit string by the *r* character:");
        foreach (string str in splitString)
        {
            Console.WriteLine(str);
        }
    }
}

Output:

String to be Split
C Sharp is A Programming Language available globally.

Split string by the *r* character:
C Sha
p is A P
og
amming Language available globally.

Use Regex.Split() to Split a String on Newline in C#

In this method, we need an extra import at the beginning of the code.

using System.Text.RegularExpressions;

The following steps are similar to those described in the previous section until the split operation. However, we will use the Regex class with the Split() function and pass the two required arguments.

One is the string to be split, stringToBeSplit, and the other is a character where the string needs to be split. All the remaining steps will be the same, showing some information, the original and split string.

Source code:

using System;
using System.Text.RegularExpressions;

public class SplitStringNLine
{
    public static void Main(string[] args)
    {
        string stringToBeSplit =
                "C Sharp " +
                "is " +
                "A " +
                "Programming " +
                "Language " +
                "available " +
                "globally.";
        string[] splitString;
        splitString = Regex.Split(stringToBeSplit, "r");
        Console.WriteLine("String to be split");
        Console.WriteLine(stringToBeSplit);
        Console.WriteLine();

        Console.WriteLine("\nSplit string by the *r* character:");
        foreach (string str in splitString)
        {
            Console.WriteLine(str);
        }
    }
}

Output:

String to be split
C Sharp is A Programming Language available globally.

Split string by the *r* character:
C Sha
p is A P
og
amming Language available globally.
Author: Saad Aslam
Saad Aslam avatar Saad Aslam avatar

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

LinkedIn

Related Article - Csharp String