Simple String split

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.

RobinS.
GoldMail.com

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');
 
M

Martin Bonner

RobinS skrev:




Jon Skeet said:
<snip>
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');

That creates two temporary copies of the string before splitting it.
The version with an array of strings will only create the split array.
 
B

Bjørn Brox

Martin 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);
....

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

That creates two temporary copies of the string before splitting it.
The version with an array of strings will only create the split array.

Correct, but the Split() variant with the split array and
SplitStringOptions parameters is not available on compact framework 3.5
which is my work platform.

Yes, I know that this is not a compact framework group, but my example
is portable... If the amount of text is small: who cares as long as it
is working :)
 
M

Martin Bonner

Martin 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);
...
String[] lines = myText.Replace("\r\n", "\n").Replace("\r",
"\n").Split('\n');
That creates two temporary copies of the string before splitting it.
The version with an array of strings will only create the split array.

Correct, but the Split() variant with the split array and
SplitStringOptions parameters is not available on compact framework 3.5
which is my work platform.

Ah, well that is somewhat of a killer argument.
Yes, I know that this is not a compact framework group, but my example
is portable... If the amount of text is small: who cares as long as it
is working :)

The trouble is that the amount of text is vunerable to growing.
Actually I don't like the way Split forces a copy of the entire string
anway (and I would have thought that was a particular problem for
compact framework). What it should really return is an IEnumerator/
IEnumerable<string> so that only has one string at a time.
 
J

Jon Skeet [C# MVP]

The trouble is that the amount of text is vunerable to growing.
Actually I don't like the way Split forces a copy of the entire string
anway (and I would have thought that was a particular problem for
compact framework). What it should really return is an IEnumerator/
IEnumerable<string> so that only has one string at a time.

I can certainly see where you're coming from - and it probably isn't
too bad in .NET 3.5 where there are quite a lot of options for dealing
with an IEnumerable<string>. However, having used Java's string
splitter (whose name I can't remember offhand) and been frustrated by
it returning an iterator, I can say it's not always ideal. (That was
partly due to the odd implementation, admittedly.)

Fortunately, in C# 2 it's reasonably easy to create your own
IEnumerable<T> implementation. Might not be a bad idea for MiscUtil...
(I've already got a LineReader for reading arbitrary TextReaders line
by line... very handy.)
 
A

Anthony Jones

Peter Duniho said:
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.


I did mean it as Jon read it. I don't think it was ambiguious I provided
the signature for the method I would have prefered. It seems to me that
some choices made in the framework were made with more 'computer science'
and less 'common sense'.
 

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