Amar Amar - 3 years ago 148
C# Question

String.split between multiple marks in a string

I'm trying to split this string so I get the values of each Title, as in 0, 200, Ownership, France, Mjaquar, Paris and 2300. If you could provide code example how to do this, would help alot!

ID: 0, Price: 2000, Legal Form: Ownership, Country: France, Street: Mjaquar, City: Paris, zip Code: 2300

Answer Source

It seems that Spliting twice (by comma and then by semicolon) will do

  string source = @"ID: 0, Price: 2000, Legal Form: Ownership, Country: France, Street: Mjaquar, City: Paris, zip Code: 2300";

  Dictionary<string, string> dict = source
    .Split(',')
    .Select(pair => pair.Split(':'))
    .ToDictionary(pair => pair[0].Trim(), 
                  pair => pair[1].Trim(), 
                  StringComparer.OrdinalIgnoreCase);

  // 2000
  Console.WriteLine(dict["price"]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download