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
sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi sbornstein2;

Here is a code snippet using a Regular Expression to do what you need.

using System.Text.RegularExpressions;

// Test String
string inputStr = "C01, C02, C03, C04, C04, C05, C06, C07, C08";
// Regex string pattern
string pattern = @"((?:[A-Z]\d{2},\s*?){6})([A-Z]\d{2},\s*?[A-Z]\d{2},\s*?[A-Z]\d{2}\s*?)";
// Get the two parts of the string
var result = Regex.Match(inputStr, pattern);
// Get the first part with six values
string strOf6 = result.Groups[1].Value;
// Remove trailing comma
strOf6 = strOf6.Substring(0, strOf6.Length - 2);
// Get the second value with 3 values
string strOf3 = result.Groups[2].Value;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOCommented:
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
0
käµfm³d 👽Commented:
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

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Gustav BrockCIOCommented:
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
0
käµfm³d 👽Commented:
@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  = )
0
käµfm³d 👽Commented:
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.
0
Gustav BrockCIOCommented:
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]);
ResultHowever, 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
0
sbornstein2Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.