Solved

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

Posted on 2006-06-09
12
6,751 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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
 
LVL 63

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 63

Expert Comment

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

749 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