Solved

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

Posted on 2006-06-09
12
6,671 Views
Last Modified: 2012-05-05
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
Comment
Question by:jasww
  • 4
  • 2
  • 2
  • +2
12 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16870597
try

retult = Foo.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
0
 

Author Comment

by:jasww
ID: 16870721
But what if my original string to split contains empty lines?  Will this remove those.  THat's not what I want to do.
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16870752
How about this:

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

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16870764
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16870773
Lol...same thing as dstanley9s....   =)
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16870873
or try:

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

This works in .Net 2.0 only.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 62

Accepted Solution

by:
Fernando Soto earned 50 total points
ID: 16871044
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
 
LVL 15

Expert Comment

by:Thogek
ID: 16871171
Have you tried something like:

    result = System.Text.RegularExpressions.Regex.Split(Foo, "\r\n");
0
 
LVL 15

Expert Comment

by:Thogek
ID: 16871178
Oops.  FernandoSoto got to it first.  :-O
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16871205
Okay, that's three solutions that will fit on one line :)  Anyone want to try a fourth?
0
 

Author Comment

by:jasww
ID: 16871340
Thanks.  THat's elegant.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 16871392
Glad I was able to help. :=)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now