Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing this string - url

Status
Not open for further replies.

elhaix

Programmer
Jul 31, 2006
115
0
0
CA
Hi,

Given the following string:

Code:
redirString = "[URL unfurl="true"]http://cgi.myserver.com/cgi-bin/clickthru.cgi?EI=58633&Q=airline&NGT=pexIB/98IeCdU9I6rcaAtz[/URL] qkeRj7ot/tlx0IWUSxx8yNmKBd7o7FmLRlNVq7K3QLe1TBC/6cN55cOCgwRpO nJ4mhED7d3Li0amkjBNhl1702 7AdO683YmGWHSdsiucv9tjZ5URI44zZVptIsIl4DWG63cM/21vxrDYkmmSd2Ldpnrap7SXcPufQuzOHFvO03MWJ 1GSjUqInpuNULwa Bu0bMVARjqZ6HRlKCz4Zwm/6VgKQh7wH5LGETIDKBja1iYEh/ODgww6L7r1s/8w==&x=1&strThisSearch=airline&bidamt=0.14&srID=211";

(remember, this is just a string)

I want to parse out the bidamt srID, etc.

I was thinking of doing something like this:
Code:
Uri uriRedir = new Uri(redirString);
... and then doing a querystring parse on it... but that's the wrong tree.

I can't exactly remember how to parse this out by "&" so I've got a key/value pair.

Can someone please give me a hand with this?


Thanks.
 
You may be able to do this using regex, but i dont remember how to do this. You can try some string manupulation, using the split function. (split takes one char)

string [] output = redirString.Split("&");

The output[0] has the link, and after that you have the params. Eg "param_name=value". To get the value of the 2nd param, do:

string param2 = output[3].Split("=");

I would suggest that you created a string function that will take as arguments the whole string and the param's name. It s eazy.. but in case you need help, ask me.

Regards
 
Typo:

string param2 = output[3].Split("=");
SHOULD BE:

string param2 = output[3].Split("=")[1];
 
i think you'll find that (really annoyingly) Split only takes arrays of chars or strings.

Code:
string[] array = someString.Split("&".ToCharArray());

i hope i'm wrong about this.
btw, is there a one-line way to pass an array of strings? something like:

Code:
string[] array = someString.Split({ "&" }, StringSplitOptions.None);

but not, because that doesn't compile.


mr s. <;)

 
Code:
string[] result = input.Split('&');

Seems to work for me.
 
In 1.1 I believe you could use a Hashtable with the method Item. That does not work with 2.0.
reference: Here is one way,
Code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

class SplitQueryString
{
    static void Main(string[] args)
    {
       string redirString = "[URL unfurl="true"]http://cgi.myserver.com/cgi-bin/clickthru.cgi?EI=58633&Q=airline&NGT=pexIB/98IeCdU9I6rcaAtz[/URL] qkeRj7ot/tlx0IWUSxx8yNmKBd7o7FmLRlNVq7K3QLe1TBC/6cN55cOCgwRpO nJ4mhED7d3Li0amkjBNhl1702 7AdO683YmGWHSdsiucv9tjZ5URI44zZVptIsIl4DWG63cM/21vxrDYkmmSd2Ldpnrap7SXcPufQuzOHFvO03MWJ 1GSjUqInpuNULwa Bu0bMVARjqZ6HRlKCz4Zwm/6VgKQh7wH5LGETIDKBja1iYEh/ODgww6L7r1s/8w==&x=1&strThisSearch=airline&bidamt=0.14&srID=211";
       Console.WriteLine("SplitQueryString");
       Dictionary<string,string> queryStringNameValue = new Dictionary<string,string>();
       queryStringNameValue = GetQueryStringParameters(redirString);
       // access a particular item
       // N.B the first name has the ? prefixed
       Console.WriteLine("queryStringNameValue ?EI: {0}", queryStringNameValue["?EI"]);
       Console.WriteLine("queryStringNameValue bidamt: {0}", queryStringNameValue["bidamt"]);
       //get all of the nvp
       Console.WriteLine("Write all of the name value pairs.");
       IDictionaryEnumerator enumeratorNameValue = queryStringNameValue.GetEnumerator();
       while (enumeratorNameValue.MoveNext())
       {
           Console.WriteLine("Key Name: {0}, Key Value: {1}", enumeratorNameValue.Key,enumeratorNameValue.Value);
       }
    }

    public static Dictionary<string, string> GetQueryStringParameters(string url)
    {
        Dictionary<string,string> nameValueTable = new Dictionary<string,string>();
        string queryString = (new Uri(url)).Query;
        string[] nameValuePairs = queryString.Split('&');
        foreach (string pair in nameValuePairs)
        {
            string[] vars = pair.Split('=');
            if (!nameValueTable.ContainsKey(vars[0]))
            {
                nameValueTable.Add(vars[0], vars[1]);
            }
        }
        return nameValueTable;
    }
}

Marty
 
the overload signature is string.Split(params char[]).

could someone explain to me why VS will transparently cast from char to char[], but not from string to string[]?

many thanks,


mr s. <;)

 
misterstick...

Ahh, right you are... had to think about that for a second.

Thanks all!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top