[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2006-06-09
12
Medium Priority
?
6,874 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
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!

 
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 64

Accepted Solution

by:
Fernando Soto earned 200 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 64

Expert Comment

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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