.NET Forum / Languages / C# / March 2008
Simple String split
|
|
Thread rating:  |
AMP - 11 Mar 2008 14:14 GMT 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
Paul E Collins - 11 Mar 2008 14:23 GMT > 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.
AMP - 11 Mar 2008 14:49 GMT On Mar 11, 9:23 am, "Paul E Collins" <find_my_real_addr...@CL4.org> wrote:
> > I am trying to split a string at the newline and this doesnt work: > > String[] Channel = FileName.Split("\r"); [quoted text clipped - 6 lines] > > Eq. Either way it doesnt work: Argument '1': cannot convert from 'string' to 'char[]' Thanks mike
Jon Skeet [C# MVP] - 11 Mar 2008 14:55 GMT > 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
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Jon Skeet [C# MVP] - 11 Mar 2008 14:33 GMT > I am trying to split a string at the newline and this doesnt work: > String[] Channel = FileName.Split("\r"); > > What am I doing wrong? It's impossible to know without knowing what the contents of FileName is.
Could you post a short but complete program which demonstrates the problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of what I mean by that.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Anthony Jones - 11 Mar 2008 14:57 GMT > 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?
 Signature Anthony Jones - MVP ASP/ASP.NET
Bjørn Brox - 11 Mar 2008 15:19 GMT Anthony Jones skrev:
>> Hello, >> I am trying to split a string at the newline and this doesnt work: [quoted text clipped - 3 lines] > > 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)
 Signature Bjørn Brox
Anthony Jones - 11 Mar 2008 21:48 GMT > Anthony Jones skrev: > > "AMP" <ampeloso@gmail.com> wrote in message news:77572b1b-5de4-4004-a2c1-93ea61ccfeda@u69g2000hse.googlegroups.com...
> >> Hello, > >> I am trying to split a string at the newline and this doesnt work: [quoted text clipped - 21 lines] > > public static String[] Str2Lines(String s, int optional_max_count) It mystifies me why that isn't an override of split as standard.
 Signature Anthony Jones - MVP ASP/ASP.NET
Jon Skeet [C# MVP] - 11 Mar 2008 21:56 GMT <snip>
> 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).
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Anthony Jones - 11 Mar 2008 23:10 GMT > <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.
 Signature Anthony Jones - MVP ASP/ASP.NET
Peter Duniho - 11 Mar 2008 23:24 GMT >> > It mystifies me why that isn't an override of split as standard. >> [quoted text clipped - 3 lines] > 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
Jon Skeet [C# MVP] - 11 Mar 2008 23:32 GMT > > I meant a simple overload of split(string delimiter). Thus > > s.split("\r\n") [quoted text clipped - 6 lines] > 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 :)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Peter Duniho - 11 Mar 2008 23:46 GMT >> 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: 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
Anthony Jones - 16 Mar 2008 09:14 GMT > >> You can just pass a single-element array and StringSplitOptions.None, > >> and [quoted text clipped - 6 lines] > 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'.
 Signature Anthony Jones - MVP ASP/ASP.NET
Jon Skeet [C# MVP] - 11 Mar 2008 23:31 GMT > > > 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. Right.
> >Could be handy, I guess - you can always add your own > > extension method (in C# 3, anyway). [quoted text clipped - 4 lines] > 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Pipo - 11 Mar 2008 21:56 GMT String[] Channel = FileName.Split(new string[] { "\\r" }, StringSplitOptions.RemoveEmptyEntries);
> Hello, > I am trying to split a string at the newline and this doesnt work: [quoted text clipped - 3 lines] > Thanks > Mike Jon Skeet [C# MVP] - 11 Mar 2008 22:01 GMT > 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.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
RobinS - 12 Mar 2008 05:16 GMT >> 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
Jon Skeet [C# MVP] - 12 Mar 2008 09:03 GMT <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);
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
RobinS - 12 Mar 2008 20:42 GMT > <snip> > [quoted text clipped - 13 lines] > ><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
Bjorn Brox - 13 Mar 2008 08:54 GMT RobinS skrev:
>> <snip> >> [quoted text clipped - 19 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');
 Signature Bjorn Brox
Martin Bonner - 13 Mar 2008 10:47 GMT > RobinS skrev: > [quoted text clipped - 27 lines] > 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.
Bjørn Brox - 13 Mar 2008 15:15 GMT >>> "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote in message ...
>>>>> If you don't know how the strings are "newlined", couldn't you test >>>>> for \n, [quoted text clipped - 3 lines] >>>>> 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 :)
 Signature Bjørn Brox
Martin Bonner - 13 Mar 2008 16:36 GMT > >>> "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote in message > ... [quoted text clipped - 16 lines] > 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.
Jon Skeet [C# MVP] - 13 Mar 2008 16:42 GMT <snip>
> > 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 [quoted text clipped - 5 lines] > 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.)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Bjorn Brox - 13 Mar 2008 09:32 GMT RobinS skrev:
>> <snip> >> [quoted text clipped - 22 lines] > 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');
 Signature Bjorn Brox
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|