Solved

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

Posted on 2006-06-09
12
6,687 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Need to pass connection string through config 2 26
Runtime Error 2 28
VB.Net How to Exit Sub - Exit Form??? 5 51
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

14 Experts available now in Live!

Get 1:1 Help Now