Click here to Skip to main content
12,950,090 members (59,424 online)
Rate this:
 
Please Sign up or sign in to 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 25-Apr-12 21:40pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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);
}
  Permalink  
v4
Comments
seshaliang 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Does this help?

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
  Permalink  
v2
Comments
VJ Reddy 26-Apr-12 3:48am
   
Good answer. 5!
seshaliang 26-Apr-12 3:51am
   
Great, very useful! Thanks!!
Mario Majcica 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 Majcica 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

The Solution 2 is very good.

But, if there are empty spaces, I think they may not be deleted.
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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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:

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

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,829
CHill60 3,460
Maciej Los 2,953
Jochen Arndt 1,975
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 26 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100