Simple String split

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
 
P

Paul E Collins

AMP said:
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.
 
A

AMP

AMP said:
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
 
J

Jon Skeet [C# MVP]

AMP said:
Either way it doesnt work:
Argument '1': cannot convert from 'string' to 'char[]'

Change "\r" to '\r' then. Although chances are you want '\n' or a split
by string rather than character, in which case you'll want to look at
Regex.Split
 
A

Anthony Jones

AMP said:
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?
 
B

Bjørn Brox

Anthony Jones skrev:
AMP said:
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)
 
A

Anthony Jones

Bjørn Brox said:
Anthony Jones skrev:
news:77572b1b-5de4-4004-a2c1-93ea61ccfeda@u69g2000hse.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.
 
P

Pipo

String[] Channel = FileName.Split(new string[] { "\\r" },
StringSplitOptions.RemoveEmptyEntries);
 
J

Jon Skeet [C# MVP]

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

To split lines? Could be handy, I guess - you can always add your own
extension method (in C# 3, anyway).
 
J

Jon Skeet [C# MVP]

Pipo said:
String[] Channel = FileName.Split(new string[] { "\\r" },
StringSplitOptions.RemoveEmptyEntries);

The "\\r" should be "\r\n" there. Otherwise you'll only split strings
which actually contain a backslash followed by an r.
 
A

Anthony Jones

Jon Skeet said:
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.
 
P

Peter Duniho

I meant a simple overload of split(string delimiter). Thus
s.split("\r\n")
would work.

It's more verbose, but what's wrong with the overload suggested by
"Pipo"? That is: String.Split(String[], StringSplitOptions)

You can just pass a single-element array and StringSplitOptions.None, and
that'd be the same as an overload that just takes a single String
parameter.

Pete
 
J

Jon Skeet [C# MVP]

Anthony Jones said:
I meant a simple overload of split(string delimiter). Thus s.split("\r\n")
would work.
Right.


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

Okay, yes, that would be a pain :(
Besides C# 3 is useful but not as useful as also having LINQ would be if the
sites had 3.5.

Yes, C# 3 is clearly better when LINQ is present.
 
J

Jon Skeet [C# MVP]

Peter Duniho said:
I meant a simple overload of split(string delimiter). Thus
s.split("\r\n")
would work.

It's more verbose, but what's wrong with the overload suggested by
"Pipo"? That is: String.Split(String[], StringSplitOptions)

You can just pass a single-element array and StringSplitOptions.None, and
that'd be the same as an overload that just takes a single String
parameter.

Yes, but I think Anthony's point is precisely that:

s.Split("\r\n")

is considerably less verbose than:

s.Split(new string[]{"\r\n"}, StringOptions.None);

I certainly know which I'd rather read :)
 
P

Peter Duniho

Yes, but I think Anthony's point is precisely that:

Maybe. I read his post to suggest that there wasn't even a Split()
overload that allowed a string to be used as a delimiter, but it was a
vaguely written statement. He could have meant it as you read it.

Pete
 
R

RobinS

Jon Skeet said:
Pipo said:
String[] Channel = FileName.Split(new string[] { "\\r" },
StringSplitOptions.RemoveEmptyEntries);

The "\\r" should be "\r\n" there. Otherwise you'll only split strings
which actually contain a backslash followed by an r.

If you don't know how the strings are "newlined", couldn't you test for \n,
\r, and \r\n? This is how I do it. Note that the order of entries in crlfs
is important.

string[] crlfs = { "\r\n", "\n", "\r" };
string[] lines = myText.Split(crlfs, StringSplitOptions.None);

RobinS.
GoldMail.com
 
J

Jon Skeet [C# MVP]

If you don't know how the strings are "newlined", couldn't you test for \n,
\r, and \r\n? This is how I do it. Note that the order of entries in crlfs
is important.

string[] crlfs = { "\r\n", "\n", "\r" };
string[] lines = myText.Split(crlfs, StringSplitOptions.None);

Unless you really need to keep empty lines, that's more easily don as:

string[] lines = myText.Split(new[] {'\r', '\n'},
StringSplitOptions.RemoveEmptyEntries);
 
R

RobinS

Jon Skeet said:
If you don't know how the strings are "newlined", couldn't you test for
\n,
\r, and \r\n? This is how I do it. Note that the order of entries in
crlfs
is important.

string[] crlfs = { "\r\n", "\n", "\r" };
string[] lines = myText.Split(crlfs, StringSplitOptions.None);

Unless you really need to keep empty lines, that's more easily don as:

string[] lines = myText.Split(new[] {'\r', '\n'},
StringSplitOptions.RemoveEmptyEntries);

<snip>


Cool. I actually DO need the empty lines. I am using it to stuff persisted
text into a multi-line textbox, so rather than "string[] lines", my code
says "myTextBox.Lines = ..."

Thanks for the tip, though. I'll keep it in a handy place.

RobinS.
GoldMail.com
 
B

Bjorn Brox

RobinS skrev:
Jon Skeet said:
If you don't know how the strings are "newlined", couldn't you test
for \n,
\r, and \r\n? This is how I do it. Note that the order of entries in
crlfs
is important.

string[] crlfs = { "\r\n", "\n", "\r" };
string[] lines = myText.Split(crlfs, StringSplitOptions.None);

Unless you really need to keep empty lines, that's more easily don as:

string[] lines = myText.Split(new[] {'\r', '\n'},
StringSplitOptions.RemoveEmptyEntries);

<snip>


Cool. I actually DO need the empty lines. I am using it to stuff
persisted text into a multi-line textbox, so rather than "string[]
lines", my code says "myTextBox.Lines = ..."

Thanks for the tip, though. I'll keep it in a handy place.

What about this, it first reduce the possible "\r\n" or "\r" instances
down to a single "\n", and then performs a split on the remaining '\n'.

String[] lines = myText.Replace("\r\n", "\n").Replace("\r",
"\n").Split("'\n');
 

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

Top