![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Converting String Pairs to IDictionary? 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(':'); Help? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Converting String Pairs to IDictionary? "coconet" <coconet@xxxxxx> wrote in message news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@xxxxxx Quote: > > 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(':'); > 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(':'); } -- Anthony Jones - MVP ASP/ASP.NET |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Converting String Pairs to IDictionary? 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@xxxxxx> wrote: Quote: >"coconet" <coconet@xxxxxx> wrote in message >news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@xxxxxx Quote: >> >> 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(':'); >} |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Converting String Pairs to IDictionary? "coconet" <coconet@xxxxxx> wrote in message news:lgvg14lsnaip1s5sq8pg2r7sirqpcfl8f1@xxxxxx Quote: > > > > 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). > > > Its not an extension method. Just because you are working on code designed to run as an instance method doesn't mean it can't call static methods. I tend to make any method that doesn't need instance members static. As a private method it doesn't really matter that much whether its marked as static or not. It just seems more correct to me. -- Anthony Jones - MVP ASP/ASP.NET Quote: > On Tue, 29 Apr 2008 22:10:00 +0100, "Anthony Jones" > <Ant@xxxxxx> wrote: > Quote: > >"coconet" <coconet@xxxxxx> wrote in message > >news:6vre14tild7q6q18tab1va3v5tm8c0cgsd@xxxxxx Quote: > >> > >> 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(':'); > >} |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Reading and assigning key/value pairs from XML | PowerShell | |||
| Cast from one generic IDictionary to another | .NET General | |||
| winrm nested key=value pairs | PowerShell | |||
| Converting Get-ChildItems to string for processing. | PowerShell | |||
| Converting date time format to string format | PowerShell | |||