sbornstein2
asked on
C# - String Parse Question
Hello all,
I have a comma separated string such as this C01, C02, C03, C04, C04, C05, C06, C07, C08
I want to split the string after 5 values so I would want two lines such as this through a loop.
C01, C02, C03, C04, C04, C05
C06, C07, C08
I have a comma separated string such as this C01, C02, C03, C04, C04, C05, C06, C07, C08
I want to split the string after 5 values so I would want two lines such as this through a loop.
C01, C02, C03, C04, C04, C05
C06, C07, C08
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could use a LINQ-based approach also:
string original = "C01, C02, C03, C04, C04, C05, C06, C07, C08";
string[] results = original.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.GroupBy(value => char.GetNumericValue(value.Last()) <= 5)
.Select(grouping => string.Join(", ", grouping))
.ToArray();
Yes, LINQ is always fun. However, you rely on the fact that one and only one entry must end with "5". If more entries, you will get strange results.
Also, LINQ seems to be quite clever, so I believe it could be reduced to:
Also, LINQ seems to be quite clever, so I believe it could be reduced to:
string[] results = original.Split(new[] {','})
.GroupBy(value => char.GetNumericValue(value.Last()) <= 5)
.Select(grouping => string.Join(", ", grouping))
.ToArray();
/gustav
@Gustav
The way that I read the question, by "five" the author meant the last digit of the groups, not that first result should be comprised of exactly five items. This is why my code is structured in this way.
As to the refactoring, it kind of works, but you'll note that it includes extra spaces between the commas and the values. You could then remove the space from the Join call, but then that leaves a leading space in front of "C06". This is why I split in the fashion that I did = )
The way that I read the question, by "five" the author meant the last digit of the groups, not that first result should be comprised of exactly five items. This is why my code is structured in this way.
As to the refactoring, it kind of works, but you'll note that it includes extra spaces between the commas and the values. You could then remove the space from the Join call, but then that leaves a leading space in front of "C06". This is why I split in the fashion that I did = )
P.S.
I don't think that LINQ is a golden hammer by any means, but I do like that you can sometimes make your code more expressive in what it's doing by wrapping it in LINQ.
I don't think that LINQ is a golden hammer by any means, but I do like that you can sometimes make your code more expressive in what it's doing by wrapping it in LINQ.
You are right. "after 5 values" can be read in that way. I thought of 5 values=entries. We'll see.
I thought as you about the space after the comma, and that was why I had to add the TrimStart() in my solution. So I was curious how LINQ would handle this. Your solution does right, but - a bit to my surprise - the reduced seems to do as well in LINQPad:
Console.WriteLine(results[ 0]);
Console.WriteLine(results[ 1]);
However, if I copy/paste it, the result is:
C01, C02, C03, C04, C04, C05
C06, C07, C08, C09
So please ignore my crippled version.
/gustav
I thought as you about the space after the comma, and that was why I had to add the TrimStart() in my solution. So I was curious how LINQ would handle this. Your solution does right, but - a bit to my surprise - the reduced seems to do as well in LINQPad:
Console.WriteLine(results[
Console.WriteLine(results[
However, if I copy/paste it, the result is:
C01, C02, C03, C04, C04, C05
C06, C07, C08, C09
So please ignore my crippled version.
/gustav
ASKER
thanks
Open in new window
That returns:Open in new window
/gustav