Home All Groups Group Topic Archive Search About
Author
26 Feb 2007 9:32 AM
nethra11
Hello,

Is it possible to split a string at the combination "\r\n"

for eg:- string str = "domain\r\nisnetwork"

Output required :-

domain
isnetwork

Author
26 Feb 2007 9:43 AM
Jon Skeet [C# MVP]
<nethr***@gmail.com> wrote:
> Is it possible to split a string at the combination "\r\n"
>
> for eg:- string str = "domain\r\nisnetwork"
>
> Output required :-
>
> domain
> isnetwork

Yes, but not with String.Split. Regex.Split will do it:

using System;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        string[] bits = Regex.Split ("hello\r\nthere\r\nJon",
                                     "\\r\\n");
        foreach (string bit in bits)
        {
            Console.WriteLine ("xxx"+bit+"xxx");
        }
    }
}

(Note that the "xxx" parts are for clarity of testing - because if it
got one bit which was "hello\r\nthere" it would still appear on two
lines!)

If you're doing this frequently in an app, it would be worth
precompiling the regular expression.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

AddThis Social Bookmark Button