string.Split for C# is crap. Is there a way to split on CrLf ?

Hi
The Split function in C# is the most annying piece of crap I have yet come across while transferring from VB.net.

I want to split on CrLf.  

It takes only a char[] as its argument, and if you pass a single-element char such as Cr, it splits on Cr but leaves a Lf at the end of each element.

If you pass a 2-element char[] array, containing Cr and Lf, then it splits on EACH character, not on BOTH characters, and one ends up with twice the number of elements as wanted.


Please advise how to do something like this:

string result[];
result = Foo.Split(Environment.NewLine);


Many thaks
Daniel
JASWW
jaswwAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi jasww;

The Regex class will do what you want. Try this.

InputString in the statement is the string to be split.

string[] splitInput = System.Text.RegularExpressions.Regex.Split(
                        InputString, "\r\n");


Fernando
0
 
dstanley9Commented:
try

retult = Foo.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
0
 
jaswwAuthor Commented:
But what if my original string to split contains empty lines?  Will this remove those.  THat's not what I want to do.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
dstanley9Commented:
How about this:

string result[];
Foo = Foo.Replace("\r\n","\n");
result = Foo.Split("\n".ToCharArray());

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could replace \r\n with \n before splitting:

            string data =
                "line1\r\n" +
                "line2\r\n" +
                "line3\r\n" +
                "\r\n" +
                "line5\r\n";          

            string[] lines = data.Replace("\r\n", "\n").Split("\n".ToCharArray());
            foreach (string line in lines)
            {
                System.Diagnostics.Debug.WriteLine(line);
            }
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...same thing as dstanley9s....   =)
0
 
dstanley9Commented:
or try:

result = Foo.Split( new String[] {"\r\n"},StringSplitOptions.None);

This works in .Net 2.0 only.

0
 
ThogekCommented:
Have you tried something like:

    result = System.Text.RegularExpressions.Regex.Split(Foo, "\r\n");
0
 
ThogekCommented:
Oops.  FernandoSoto got to it first.  :-O
0
 
dstanley9Commented:
Okay, that's three solutions that will fit on one line :)  Anyone want to try a fourth?
0
 
jaswwAuthor Commented:
Thanks.  THat's elegant.
0
 
Fernando SotoRetiredCommented:
Glad I was able to help. :=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.