开发者

How to quickly parse the following data into a data structure

I have data like

A=B&C=D&E=F

How to quickly parse it into a data structure like a dic开发者_JAVA技巧tionary? Any built-in support from .net framework?


    var input = "A=B&C=D&E=F";
    var output = input
                    .Split(new string[] {"&"}, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => s.Split('=', 2))
                    .ToDictionary(d => d[0], d => d[1]);


Use the HttpUtility.ParseQueryString()-Method:

http://msdn.microsoft.com/en-us/library/ms150046.aspx

It will return a NameValueCollection, which is very similary to a dictionary.


Yep, LINQ is your friend here.

string myString = "A=B&C=D&E=F";

var dictionary = myString.Split('&')
  .Select(pair => pair.Split('='))
  .ToDictionary (array => array[0], array => array[1]);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜