Search code examples
c#parsingnvp

Is there a native way to parse key/value pairs into an array?


I have a string that is the contents of a webpage in this format :

   value=foo&value1=bar&value2=foobar

What I would like to know is, is there a built-in way to convert this into an object/list of object/whatever so that I can loop through or access the values by keyname

I know I can split on '&', and then split on '=', but I believe there is a built-in way to do this, I just do not know what it is.

The language I am working in is C#.


Solution

  • You can use HttpUtility.ParseQueryString.

    Eg.

    NameValueCollection values = HttpUtility.ParseQueryString("value=foo&value1=bar&value2=foobar");