How to split strings on carriage return with C#?

C#Text Manipulation

C# Problem Overview


I have an ASP.NET page with a multiline textbox called txbUserName. Then I paste into the textbox 3 names and they are vertically aligned:

  • Jason
  • Ammy
  • Karen

I want to be able to somehow take the names and split them into separate strings whenever i detect the carriage return or the new line. i am thinking that an array might be the way to go. Any ideas?

thank you.

C# Solutions


Solution 1 - C#

string[] result = input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);

This covers both \n and \r\n newline types and removes any empty lines your users may enter.

I tested using the following code:

        string test = "PersonA\nPersonB\r\nPersonC\n";
        string[] result = test.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
        foreach (string s in result)
            Console.WriteLine(s);

And it works correctly, splitting into a three string array with entries "PersonA", "PersonB" and "PersonC".

Solution 2 - C#

Replace any \r\n with \n, then split using \n:

string[] arr = txbUserName.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());

Solution 3 - C#

Take a look at the String.Split function (not sure of exact syntax, no IDE in front of me).

string[] names = txbUserName.Text.Split(Environment.Newline);

Solution 4 - C#

String.Split?

mystring.Split(new Char[] { '\n' })

Solution 5 - C#

using System.Text;
using System.Text.RegularExpressions;


 protected void btnAction_Click(object sender, EventArgs e)
    {
        string value = txtDetails.Text;
        char[] delimiter = new char[] { ';','[' };
        string[] parts = value.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < parts.Length; i++)
        {
            txtFName.Text = parts[0].ToString();
            txtLName.Text = parts[1].ToString();
            txtAge.Text = parts[2].ToString();
            txtDob.Text = parts[3].ToString();
        }
    }

Solution 6 - C#

Try this:

message.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

Works if :

var message = "test 1\r\ntest 2";

Or

var message = "test 1\ntest 2";

Or

var message = "test 1\rtest 2";

Solution 7 - C#

It depends what you want to do. Another option, which is probably overkill for small lists, but may be more memory efficient for larger strings, is to use the StringReader class and use an enumerator:

IEnumerable<string> GetNextString(string input)
{
    using (var sr = new StringReader(input))
    {
        string s;
        while ((s = sr.ReadLine()) != null)
        {
            yield return s;
        }
    }
}

This supports both \n and \r\n line-endings. As it returns an IEnumerable you can process it with a foreach, or use any of the standard linq extensions (ToList(), ToArray(), Where, etc).

For example, with a foreach:

var ss = "Hello\nworld\r\ntwo bags\r\nsugar";
foreach (var s in GetNextString(ss))
{
    Console.WriteLine("==> {0}", s);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEricaView Question on Stackoverflow
Solution 1 - C#jasonhView Answer on Stackoverflow
Solution 2 - C#o.k.wView Answer on Stackoverflow
Solution 3 - C#TryCatchView Answer on Stackoverflow
Solution 4 - C#ThanatosView Answer on Stackoverflow
Solution 5 - C#RubiView Answer on Stackoverflow
Solution 6 - C#TovichView Answer on Stackoverflow
Solution 7 - C#Chris JView Answer on Stackoverflow