C# String Parse Question

Hello EE Community,

I have a string list that has a pipe delimiter between a customer code and a file path.   I have to get the right split of the | where I find by the customer code.  i.e.

List<string>
[0]  ABC|C:\Customer\ABCCorp
[1]  DEF|C:\Customer\DefLance

If I need to find the path for DEF how can I get this?
sbornstein2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Bill PrewIT / Software Engineering ConsultantCommented:
Are you stuck with a list, it feels like a dictionary would be a better choice?  Assuming this is essentially a "table" of customer codes, and their associated paths?  In a dictionary structure you could access the DEF "key" directly and get the path, ass opposed to having to search the list manually looking for it at the left of the value.


»bp
0
sbornstein2Author Commented:
Not necessarily I have a string that is formatted like this:

ABC|C:\Customer\ABCCorp,DEF|C:\Customer\DefLance
0
Bill PrewIT / Software Engineering ConsultantCommented:
In your original question, as far as parsing the "key" and "value" from list entries, you can do something like below.  Naturally you could have split the list strings into a temp array as well, I just chose to use one less variable to give you the idea...

        Custs.Add("ABC|C:\\Customer\\ABCCorp");
        Custs.Add("DEF|C:\\Customer\\DefLance");
        foreach (var c in Custs) {
            Console.WriteLine("Key {0}, Value {1}", c.Split('|')[0], c.Split('|')[1]);
        }

Open in new window


»bp
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Bill PrewIT / Software Engineering ConsultantCommented:
Not necessarily I have a string that is formatted like this:

ABC|C:\Customer\ABCCorp,DEF|C:\Customer\DefLance

Okay, how are you loading that into a list now, what does that code look like?  Are you splitting on ',' ?


»bp
0
sbornstein2Author Commented:
I am splitting on comma into a string list:

 List<string> customers = customers.Split(",").ToList();
0
Bill PrewIT / Software Engineering ConsultantCommented:
So here's a basic approach of a dictionary.  You can streamline it a bit by more "interesting" code, but I wanted to show the mechanics at work.  See if this makes sense and might help...

        string input = "ABC|C:\\Customer\\ABCCorp,DEF|C:\\Customer\\DefLance";
        Dictionary<string, string> Custs = new Dictionary<string, string>();
        string[] entry = input.Split(',');
        foreach (string pair in entry) {
            string[] field = pair.Split('|');
            Custs.Add(field[0], field[1]);
        }
        Console.WriteLine("{0}", Custs["DEF"]);

Open in new window


»bp
0
Fernando SotoRetiredCommented:
Hi sbornstein2;

Will something like this do?
// Test Data
List<string> customers = new List<string>() {@"ABC|C:\Customer\ABCCorp", @"DEF|C:\Customer\DefLance"};

// Used to find the item that is needed
string findCust = "def";
string path = customers.Where(c => c.StartsWith(findCust, StringComparison.InvariantCultureIgnoreCase))
                       .Select(c => c.Substring(c.IndexOf('|') + 1)).FirstOrDefault();

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
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.