[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6903
  • Last Modified:

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
0
jasww
Asked:
jasww
  • 4
  • 2
  • 2
  • +2
1 Solution
 
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
 
dstanley9Commented:
How about this:

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
Fernando SotoCommented:
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
 
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 SotoCommented:
Glad I was able to help. :=)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now