• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 47
  • Last Modified:

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?
0
sbornstein2
Asked:
sbornstein2
  • 4
  • 2
3 Solutions
 
Bill PrewCommented:
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 PrewCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Bill PrewCommented:
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 PrewCommented:
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
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.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now