Re: Converting String Pairs to IDictionary? by coconet
coconet
Wed Apr 30 09:12:38 CDT 2008
Thanks for the help. Unfortunately I am working with an instance class
so i can't do an extension method (I think that's what you're doing).
On Tue, 29 Apr 2008 22:10:00 +0100, "Anthony Jones"
<Ant@yadayadayada.com> wrote:
>"coconet" <coconet@community.nospam> wrote in message
>news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@4ax.com...
>>
>> I have a string like this
>>
>> mystring = "color1:blue;color2:red";
>>
>> I am tring to convert this into an IDictionary populated like this:
>> color1 blue
>> color2 red
>>
>> My non-working syntax is
>>
>> IDictionary<string,string> tempdict =
>> new Dictionary<string>(mystring.Split(';').ToDictionary()
>> ).Split(':');
>>
>
>Try this:- (untested)
>
>IDictionary<string,string> tempdict = new Dictionary<string,string>()
>foreach(string[] item in splitItems(mystring.Split(';')))
> tempdict.Add(item[0], item[1]);
>
>private static IEnumerable<string[]> splitItems(string input)
>{
> foreach (string item in input.Split(';'))
> yield return item.Split(':');
>}