String.Split()

  • Thread starter Thread starter nethra11
  • Start date Start date
N

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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top