Converting a QueryString-TYPE string to a NameValueCollection

Y

Yuri Vanzine

Saw this requested awhile ago on Matthew Reynolds's blog here:

http://www.dotnet247.com/247reference/msgs/11/57944.aspx

Problem: often times I deal with querystring-formatted strings w/o the
actual HttpRequest object involved.
Solution: the function accepts such a querystring-type string and outputs a
NameValueCollection object


public static NameValueCollection NameValueCollection(string qs)
{
NameValueCollection nvc = new NameValueCollection();
//strip string data before the question mark
qs = qs.IndexOf('?')>0?qs.Remove(0,qs.IndexOf('?')+1):qs;
Array sqarr = qs.Split("&".ToCharArray());
for(int i=0;i<sqarr.Length;i++)
{
string [] pairs =
sqarr.GetValue(i).ToString().Split("=".ToCharArray());
nvc.Add(pairs[0],pairs[1]);
}
return nvc;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top