开发者

Is it possible to split a string into a dictionary?

Is it possible to s开发者_开发百科plit 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>


Duplicate of your own question...

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.


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]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜