tags:

views:

95

answers:

2

Is it possible to split a string by '&' and init a dictionary in one go or you have to use a string array first?

I want to take the url part:

?a=2&b=3

and load a dictionary<string,string>

+3  A: 

Duplicate of your own question...

http://stackoverflow.com/questions/2180004/best-way-to-take-all-querystring-pairs-and-init-a-dictionary

As womp said, HttpUtility.ParseQueryString() is your best bet.

Edit:
After doing a little digging I found the following extension method NameValueCollectionExtensions.CopyTo(this NameValueCollection, IDictionary< string, object >) that you could use to populate a true IDictionary.

JohannesH
+1  A: 

Based on the format given, I think you want something like this:

var dict = queryString
    .Substring(1) // to skip the "?"
    .Split("&")
    .Select(s => s.Split("="))
    .ToDictionary(sa => sa[0], sa => sa[1]);
Sean Devlin
Actually, the library method JohannesH mentions is probably a better bet.
Sean Devlin