19

If I have a string like so:

"Name=Irwin&Home=Caribbean&Preference=Coffee"

is there a method in C# that can convert that to a key-value pair similar to Request.QueryString?

Irwin
  • 12,551
  • 11
  • 67
  • 97

5 Answers5

41

You can try using HttpUtility.ParseQueryString.

var nvc = HttpUtility.ParseQueryString(yourString);
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • 1
    No need to new up a NVC, as `ParseQueryString` returns that type already. – Oded Mar 12 '12 at 15:33
  • But afaik it's not part of the Client Profile, that might be an issue. (Posted before I spotted the ASP.NET tag). – H H Mar 12 '12 at 15:44
  • When you create code example, please avoid "var" as it can make the reader confused of what type is returned. Please specify "string" if this is the case? – einord Feb 04 '15 at 10:59
19

And now, for the longest LINQ expression...

var dict = "Name=Irwin&Home=Caribbean&Preference=Coffee"
    .Split('&')
    .Select(p => p.Split('='))
    .ToDictionary(p => p[0], p => p.Length > 1 ? Uri.UnescapeDataString(p[1]) : null);

But be aware that it will throw if there are multiple keys with the same name.

If you want to protected yourself from this add:

    .GroupBy(p => p[0]).Select(p => p.First())

just before the .ToDictionary (and after the .Select)

this will take the first key=value of the multiple ones. Change .First() to .Last() to take the last one.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • +1 In my case, more helpful than the accepted answer as I'm looking for the case of parsing a style= string of key:value[;key:value]... – Bob Kaufman Sep 27 '12 at 20:14
  • Actually, that’s not all that long of a LINQ expression :-p. Also, if this really was intending to parse a query-like string, where’re the calls to [`Uri.UnescapeDataString()`](https://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring%28v=vs.110%29.aspx)? – binki Mar 07 '16 at 18:57
  • 1
    @binki You are right (on the second one) :-) Added... On the lengthness, posterity will decide :-) – xanatos Mar 07 '16 at 19:37
  • Almost—the key could be written in escaped from in a query string as well ;-) – binki Mar 07 '16 at 20:47
7

You can also use the ToDictionary() method:

var input = "Name=Irwin&Home=Caribbean&Preference=Coffee";
var items = input.Split(new[] { '&' });
var dict = items.Select(item => item.Split(new[] {'='})).ToDictionary(pair => pair[0], pair => pair[1]);
Abbas
  • 14,186
  • 6
  • 41
  • 72
3

You are looking for

System.Web.HttpUtility.ParseQueryString

PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
1

You should be able to split this string based on the ampersands and equals signs, then feed each one into a new KeyValuePair:

Dictionary<string, string> myValues = new Dictionary<string,string>();

string[] elements = myString.Split('=','&');

for(int i=0;i<elements.Length; i+=2)
{
   myValues.Add(elements[i], elements[i+1]);
}

This is simplistic and makes a lot of assumptions about the format of your string, but it works for your example.

KeithS
  • 70,210
  • 21
  • 112
  • 164