Link to home
Start Free TrialLog in
Avatar of sbornstein2
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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I prefer string handling for such tasks:
string list = "C01, C02, C03, C04, C04, C05, C06, C07, C08";
char[] separator = new char[] {','};
string[] items = list.Split(separator);

string fiveItems = string.Join(separator[0].ToString(), items, 0, 5);
string lastItems = string.Join(separator[0].ToString(), items, 5, items.Length - 5).TrimStart();

Console.WriteLine("All : " + list);
Console.WriteLine("Five: " + fiveItems);
Console.WriteLine("Last: " + lastItems);

Open in new window

That returns:
All : C01, C02, C03, C04, C04, C05, C06, C07, C08
Five: C01, C02, C03, C04, C04
Last: C05, C06, C07, C08

Open in new window

/gustav
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();

Open in new window

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:
string[] results = original.Split(new[] {','})
                           .GroupBy(value => char.GetNumericValue(value.Last()) <= 5)
                           .Select(grouping => string.Join(", ", grouping))
                           .ToArray();

Open in new window

/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  = )
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.
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]);
User generated imageHowever, 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
Avatar of sbornstein2
sbornstein2

ASKER

thanks