Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Is there a simple way to split a long string into servals by the line terminator ("\r\n") in C# code?

Thanks
Posted

C#
string bla = "abc\r\ndef\r\nghijk";
string [] splitter = { "\r\n" };
string [] foo = bla.Split(splitter, StringSplitOptions.RemoveEmptyEntries);


or with Streams, probably you get this string out of a file. But this only works in cases where you habe CR/LF "Splitters".

C#
string bla = "abc\r\ndef\r\nghijk";
using (StringReader sr = new StringReader(bla))
{
    string line = string.Empty;
    do
    {
        line = sr.ReadLine();

        if (line != null)
        {
            Debug.WriteLine(line);

        }

    } while (line != null);
}
 
Share this answer
 
v4
Comments
sesha Fan 26-Apr-12 3:51am    
There will be empty string between every two items. But any way, thanks very much.
Andy411 26-Apr-12 3:54am    
I just recognized it, too. I improved my solution with string instead of charackters to solve this problem.
VJ Reddy 26-Apr-12 4:01am    
Just now I have observed, after posting my solution that you have modified your solution similarly. Now, it is very good. My 5!
Andy411 26-Apr-12 4:09am    
Thx
Does this help?

C#
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
    string value = "cat\r\ndog\r\nanimal\r\nperson";
    //
    // Split the string on line breaks.
    // ... The return value from Split is a string[] array.
    //
    string[] lines = Regex.Split(value, "\r\n");

    foreach (string line in lines)
    {
        Console.WriteLine(line);
    }
    }
}



Also check this page out:
http://www.dotnetperls.com/split[^]

Cheers
 
Share this answer
 
v2
Comments
VJ Reddy 26-Apr-12 3:48am    
Good answer. 5!
sesha Fan 26-Apr-12 3:51am    
Great, very useful! Thanks!!
Mario Majčica 26-Apr-12 3:52am    
Glad to help. If helpful accept and eventually vote the answer. ;)

Cheers
Andy411 26-Apr-12 3:54am    
Great idea to do with regex
Mario Majčica 26-Apr-12 3:56am    
It's a nice and neat idea but not the most efficient. However if performances are not an issue, it's a clean solution.

Cheers
The Solution 2 is very good.

But, if there are empty spaces, I think they may not be deleted.
C#
void Main()
    {
    string input= "First Line\r\nSecond Line\r\n\r\nThird Line";
    string[] lines = input.Split(new string[]{"\r\n"},
                    StringSplitOptions.RemoveEmptyEntries);
    foreach (string line in lines)
    {
        Console.WriteLine (line);
    }
}
//First Line
//Second Line
//Third Line

In the sample shown above after SecondLine there is an empty entry due to repition of \r\n. With StringSplitOptions.RemoveEmptyEntries option this empty entry can removed.
 
Share this answer
 
It's not too dificult to do that.
It does depend however how you want to split it up...

Here's an example of either 2 ways i think would suit your problem:

C#
int charsToSplit = 9;
string strng = "teststring that is quite long. When do you want to cut it up?"

strng.Insert(charsToSplit, "/r/n"); // this, you could use to cut the string after a number of characters

strng.Replace(".", "./r/n");//this, you could use if you want a linebreak after every point-character


hope this helps you! ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900