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:
    731
    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:
    421
    Shaji
    Aug 28, 2003
  3. anon

    Confused by the NameValueCollection

    anon, May 13, 2004, in forum: Microsoft ASP .NET
    Replies:
    5
    Views:
    369
    Chris R. Timmons
    May 14, 2004
  4. Julia
    Replies:
    1
    Views:
    364
    Girish bharadwaj
    Sep 23, 2004
  5. Guest
    Replies:
    3
    Views:
    852
    Karl Seguin
    Dec 23, 2004
  6. sonic
    Replies:
    1
    Views:
    351
    John Saunders
    Jan 7, 2005
  7. John Dalberg
    Replies:
    0
    Views:
    201
    John Dalberg
    Jan 21, 2005
  8. Mehdi
    Replies:
    6
    Views:
    596
    sloan
    Apr 6, 2006
Loading...