Converting a QueryString-TYPE string to a NameValueCollection

Discussion in 'Microsoft ASP .NET' started by Yuri Vanzine, Aug 18, 2003.

  1. Yuri Vanzine

    Yuri Vanzine Guest

    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;
    }
     
    Yuri Vanzine, Aug 18, 2003
    #1
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Miller
    Replies:
    1
    Views:
    543
    Mark Miller
    Jul 18, 2003
  2. Alfons Puig

    NameValueCollection.GetValues and array especification

    Alfons Puig, Aug 28, 2003, in forum: Microsoft ASP .NET
    Replies:
    1
    Views:
    352
    Shaji
    Aug 28, 2003
  3. Ken Cox [Microsoft MVP]

    Re: NameValueCollection or Hashtable values

    Ken Cox [Microsoft MVP], Mar 25, 2004, in forum: Microsoft ASP .NET
    Replies:
    0
    Views:
    288
    Ken Cox [Microsoft MVP]
    Mar 25, 2004
  4. sonic
    Replies:
    1
    Views:
    277
    John Saunders
    Jan 7, 2005
  5. Mehdi
    Replies:
    6
    Views:
    380
    sloan
    Apr 6, 2006
Loading...

Share This Page