>
>Now that we are clear on what Michel actually wants in the result the shortest solution seems to be:>>string[] delimiters = new string[] { Environment.NewLine }; //may be safer than "\r\n" ?
>>string[] lines = TestString.Split(delimiters,StringSplitOptions.None);
>>Again I don't know if performance will be an issue but with 100000 lines of about 100 characters each this is still a sub-millisecond operation on my (average) machine. Ran out of ram trying a million lines tho :-{
>>Update: Your version using fluent syntax took pretty much the same time:string[] lines = TestString.Replace("\r",String.Empty).Split("\n".ToCharArray());
>
>Thanks for taking the time to benchmark it, Viv. =0)
Milliseconds I can spare (g)
Actually, FWIW, I just tried each option in a 1000 loop.
Splitting on a char[] and removing empty lines was the fastest (17secs) but that wouldn't suit Michel's needs.
Next fastest was the string[] split that I suggested (20 secs)
Using a replace to drop one of the CRLF characters before a char[] split was slower (28secs)
>>
>>>Wait a minute, I've come up with another iteration of this and this time it will not remove your empty lines. Dunno why I didn't think of it earlier:
>>>
>>>
>>>string CRLF = "\r\n";
>>>string CR = "\r";
>>>string LF = "\n";
>>>string TestString = string.Format("Test this string. {0}It contains {1}{2}a blank line.", CRLF, CRLF, CRLF);
>>>// first replace the LFs
>>>TestString = TestString.Replace(LF, "");
>>>// then split on the CRs
>>>string[] lines = TestString.Split(CR.ToCharArray());
>>>
>>>for (int i = 0; i < lines.Length; i++)
>>>{
>>> Debug.WriteLine(lines[i]);
>>>}
>>>
>>>
>>>The only thing I haven't tested is performance, but since you have the large strings, I'll leave that up to you.
>>>
>>>~~Bonnie
>>>
>>>
>>>
>>>
>>>>>Well, that is weird behavior (the thing with the LF). If your empty lines contain more than just a CRLF (like, say they contain a space or a tab), then my example will retain that "empty" line. But, anyway, it doesn't matter ... it sounds like you found a workable solution.
>>>>
>>>>Yes, it is just that I have to scan all the lines, after I have populated the string builder, to remove the LF character. Like I said, this does not happen on the first line but only on all upcoming ones.