473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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,stringtempdict =
new Dictionary<string>(mystring.Split(';').ToDictionar y()
).Split(':');

Help?
Jun 27 '08 #1
3 1500
"coconet" <co*****@community.nospamwrote in message
news:6v********************************@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,stringtempdict =
new Dictionary<string>(mystring.Split(';').ToDictionar y()
).Split(':');
Try this:- (untested)

IDictionary<string,stringtempdict = 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
Jun 27 '08 #2

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"
<An*@yadayadayada.comwrote:
>"coconet" <co*****@community.nospamwrote in message
news:6v********************************@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,stringtempdict =
new Dictionary<string>(mystring.Split(';').ToDictionar y()
).Split(':');

Try this:- (untested)

IDictionary<string,stringtempdict = 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(':');
}
Jun 27 '08 #3
"coconet" <co*****@community.nospamwrote in message
news:lg********************************@4ax.com...
>
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
On Tue, 29 Apr 2008 22:10:00 +0100, "Anthony Jones"
<An*@yadayadayada.comwrote:
"coconet" <co*****@community.nospamwrote in message
news:6v********************************@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,stringtempdict =
new Dictionary<string>(mystring.Split(';').ToDictionar y()
).Split(':');
Try this:- (untested)

IDictionary<string,stringtempdict = 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(':');
}

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: M C | last post by:
Hi, This is a problem that has been haunting me for days and I've come to a complete dead-end. I'm using a objectdatasource to select and update a gridview control. Populating with select works...
7
by: M C | last post by:
Hi, This is a problem that has been haunting me for days and I've come to a complete dead-end. I'm using a objectdatasource to select and update a gridview control. Populating with select works...
8
by: M C | last post by:
Hi, This is a problem that has been haunting me for days and I've come to a complete dead-end. I'm using a objectdatasource to select and update a gridview control. Populating with select works...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.