Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# string
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 22: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 at 26-Apr-12 3:51am
   
There will be empty string between every two items. But any way, thanks very much.
Andy411 at 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 at 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 at 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 at 26-Apr-12 3:48am
   
Good answer. 5!
seshaliang at 26-Apr-12 3:51am
   
Great, very useful! Thanks!!
Mario Majcica at 26-Apr-12 3:52am
   
Glad to help. If helpful accept and eventually vote the answer. ;)
 
Cheers
Andy411 at 26-Apr-12 3:54am
   
Great idea to do with regex
Mario Majcica at 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! Wink | ;)
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 26 Apr 2012
Copyright © CodeProject, 1999-2014
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