Solved

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

Posted on 2006-06-09
12
6,785 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
Technology Partners: 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 86

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 86

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

691 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