By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

Simple String split

P: n/a
AMP
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");

What am I doing wrong?
Thanks
Mike
Mar 11 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"AMP" <am******@gmail.comwrote:
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
What am I doing wrong?
Depending on where you got it, the newline sequence might also be \n or
\r\n

Try studying the variable with a Watch in the debugger.

Eq.
Mar 11 '08 #2

P: n/a
AMP
On Mar 11, 9:23*am, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
"AMP" <ampel...@gmail.comwrote:
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
What am I doing wrong?

Depending on where you got it, the newline sequence might also be \n or
\r\n

Try studying the variable with a Watch in the debugger.

Eq.
Either way it doesnt work:
Argument '1': cannot convert from 'string' to 'char[]'
Thanks
mike
Mar 11 '08 #3

P: n/a

"AMP" <am******@gmail.comwrote in message
news:77**********************************@u69g2000 hse.googlegroups.com...
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
Try:-

String[] Channel = FileName.Split('\r');

Also are you using the Visual Studio?

--
Anthony Jones - MVP ASP/ASP.NET
Mar 11 '08 #4

P: n/a
Anthony Jones skrev:
"AMP" <am******@gmail.comwrote in message
news:77**********************************@u69g2000 hse.googlegroups.com...
>Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");

Try:-

String[] Channel = FileName.Split('\r');
To be sure use \n instead of \r because newlines in a text file created
on a unix/linux system a text line is terminated by a single "\n", while
in DOS/Windows it is "\r\n".

String[] Channel = FileName.Split('\n');

Secondly: If the text line is from a DOS/Windows text file you still
have to remove the remaining \r or \n depending on what separator you
use in the split statement.

String arg0 = Channel[0].trim("\r\n");

and so on....

Personally I ended up in implementing this function:

public static String[] Str2Lines(String s, int optional_max_count)

--
Bjørn Brox
Mar 11 '08 #5

P: n/a
"Bjørn Brox" <bp****@gmail.comwrote in message
news:47********@news.broadpark.no...
Anthony Jones skrev:
"AMP" <am******@gmail.comwrote in message
news:77**********************************@u69g2000 hse.googlegroups.com...
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
Try:-

String[] Channel = FileName.Split('\r');
To be sure use \n instead of \r because newlines in a text file created
on a unix/linux system a text line is terminated by a single "\n", while
in DOS/Windows it is "\r\n".

String[] Channel = FileName.Split('\n');

Secondly: If the text line is from a DOS/Windows text file you still
have to remove the remaining \r or \n depending on what separator you
use in the split statement.

String arg0 = Channel[0].trim("\r\n");

and so on....

Personally I ended up in implementing this function:

public static String[] Str2Lines(String s, int optional_max_count)
It mystifies me why that isn't an override of split as standard.
--
Anthony Jones - MVP ASP/ASP.NET
Mar 11 '08 #6

P: n/a
String[] Channel = FileName.Split(new string[] { "\\r" },
StringSplitOptions.RemoveEmptyEntries);
"AMP" <am******@gmail.comschreef in bericht
news:77**********************************@u69g2000 hse.googlegroups.com...
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");

What am I doing wrong?
Thanks
Mike
Mar 11 '08 #7

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Anthony Jones <An*@yadayadayada.comwrote:

<snip>
It mystifies me why that isn't an override of split as standard.

To split lines?
I meant a simple overload of split(string delimiter). Thus s.split("\r\n")
would work.
>Could be handy, I guess - you can always add your own
extension method (in C# 3, anyway).

That would be nice but most of our customers have only just moved to 2.0 and
I'm still wary of using C# 3 in that environment. As yet I've not
discovered a way to ensure ASPX pages and App_Code files don't go out to
these customers with C# 3 code in them. So at the moment I'm having to
stick with C# 2. :(

Besides C# 3 is useful but not as useful as also having LINQ would be if the
sites had 3.5.
--
Anthony Jones - MVP ASP/ASP.NET
Mar 11 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.