string folders = @"32;c:\|48;c:\code|52;c:\program files|";
I created the following method that returns a Dictionary
public static Dictionary<int, string> GetFolders(string folders) { string[] x = folders.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); // first retrieve a string array of all the pairs return x.ToDictionary(y => Convert.ToInt32(y.Split(';')[0]), y => y.Split(';')[1]); // second stage of splitting }
Here's a full working console example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string folders = @"30;c:\|48;c:\code|99;c:\Program Files"; Dictionary<int, string> KeyValuePairs = GetFolders(folders); foreach (KeyValuePair<int, string> pair in KeyValuePairs) { Console.WriteLine("Key: '" + pair.Key + "', Value: '" + pair.Value + "'"); } Console.ReadKey(); /* Results: * * Key: '30', Value: 'c:\' * Key: '48', Value: 'c:\code' * Key: '99', Value: 'c:\Program Files' */ } public static Dictionary<int, string> GetFolders(string folders) { string[] x = folders.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); // first retrieve a string array of all the pairs return x.ToDictionary(y => Convert.ToInt32(y.Split(';')[0]), y => y.Split(';')[1]); // second stage of splitting } } }
No comments:
Post a Comment