14

I am reading from history, and I want that when i come across a google query, I can extract the query string. I am not using request or httputility since i am simply parsing a string. however, when i come across URLs like this, my program fails to parse it properly:

http://www.google.com.mt/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&channel=s&hl=mt&source=hp&biw=986&bih=663&q=hotmail&meta=&btnG=Fittex+bil-Google

what i was trying to do is get the index of q= and the index of & and take the words in between but in this case the index of & will be smaller than q= and it will give me errors.

any suggestions?

thanks for your answers, all seem good :) p.s. i couldn't use httputility, not I don't want to. when i add a reference to system.web, httputility isn't included! it's only included in an asp.net application. Thanks again

2
  • So look for & which is immediately after q..... Commented May 21, 2011 at 15:26
  • To include HttpUtility, you just need to add a reference to the System.Web assembly in your project. It doesn't have to be an asp.net application. Commented May 21, 2011 at 16:23

8 Answers 8

24

It's not clear why you don't want to use HttpUtility. You could always add a reference to System.Web and use it:

var parsedQuery = HttpUtility.ParseQueryString(input);
Console.WriteLine(parsedQuery["q"]);

If that's not an option then perhaps this approach will help:

var query = input.Split('&')
                 .Single(s => s.StartsWith("q="))
                 .Substring(2);
Console.WriteLine(query);

It splits on & and looks for the single split result that begins with "q=" and takes the substring at position 2 to return everything after the = sign. The assumption is that there will be a single match, which seems reasonable for this case, otherwise an exception will be thrown. If that's not the case then replace Single with Where, loop over the results and perform the same substring operation in the loop.

EDIT: to cover the scenario mentioned in the comments this updated version can be used:

int index = input.IndexOf('?');
var query = input.Substring(index + 1)
                 .Split('&')
                 .SingleOrDefault(s => s.StartsWith("q="));

if (query != null)
    Console.WriteLine(query.Substring(2));
Sign up to request clarification or add additional context in comments.

1 Comment

This, too, will fail when the url contains & in the path part. For example, http://example.com/ugly&but&legal&url&q=1234?q=5678.
7

here is the solution -

string GetQueryString(string url, string key)
{
    string query_string = string.Empty;

    var uri = new Uri(url);
    var newQueryString = HttpUtility.ParseQueryString(uri.Query);
    query_string = newQueryString[key].ToString();

    return query_string;
}

Comments

6

If you don't want to use System.Web.HttpUtility (thus be able to use the client profile), you can still use Mono HttpUtility.cs which is only an independent .cs file that you can embed in your application. Then you can simply use the ParseQueryString method inside the class to parse the query string properly.

Comments

3

Why don't you create a code which returns the string from the q= onwards till the next &?

For example:

string s = historyString.Substring(url.IndexOf("q="));

int newIndex = s.IndexOf("&");

string newString = s.Substring(0, newIndex);

Cheers

1 Comment

And then watch it fail when presented with http://www.example.com/testo/thisq=99&xyzzy/hello?q=99
2

Use the tools available:

String UrlStr = "http://www.google.com.mt/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&channel=s&hl=mt&source=hp&biw=986&bih=663&q=hotmail&meta=&btnG=Fittex+bil-Google";

NameValueCollection Items = HttpUtility.ParseQueryString(UrlStr);

String QValue = Items["q"];

Comments

2

If you really need to do the parsing yourself, and are only interested in the value for 'q' then the following would work:

        string url = @"http://www.google.com.mt/search?" +
            "client=firefoxa&rls=org.mozilla%3Aen-" +
            "US%3Aofficial&channel=s&hl=mt&source=hp&" +
            "biw=986&bih=663&q=hotmail&meta=&btnG=Fittex+bil-Google";

        int question = url.IndexOf("?");
        if(question>-1)
        {
            int qindex = url.IndexOf("q=", question);
            if (qindex > -1)
            {
                int ampersand = url.IndexOf('&', qindex);
                string token = null;

                if (ampersand > -1)
                    token = url.Substring(qindex+2, ampersand - qindex - 2);
                else
                    token = url.Substring(qindex+2);

                Console.WriteLine(token);
            }
        }

But do try to look at using a proper URL parser, it will save you a lot of hassle in the future.

(amended this question to include a check for the '?' token, and support 'q' values at the end of the query string (without the '&' at the end) )

2 Comments

That's going to fail when there's q "q=" or an ampersand in the path part of the url. Like this: example.com/testo/thisq=99&xyzzy/hello?q=99. Yes, that's legal. You first have to find the query string indicator ('?'), and base everything else from that.
Very true! I will amend the answer.
1

And that's why you should use Uri and HttpUtility.ParseQueryString.

Comments

1

HttpUtility is fine for the .Net Framework. However that class is not available for WinRT apps. If you want to get the parameters from a url in a Windows Store App you need to use WwwFromUrlDecoder. You create an object from this class with the query string you want to get the parameters from, the object has an enumerator and supports also lambda expressions.

Here's an example

var stringUrl = "http://localhost/?name=Jonathan&lastName=Morales";
var decoder = new WwwFormUrlDecoder(stringUrl);
//Using GetFirstByName method
string nameValue = decoder.GetFirstByName("name");
//nameValue has "Jonathan"

//Using Lambda Expressions
var parameter = decoder.FirstOrDefault(p => p.Name.Contains("last")); //IWwwFormUrlDecoderEntry variable type
string parameterName = parameter.Name; //lastName
string parameterValue = parameter.Value; //Morales

You can also see http://www.dzhang.com/blog/2012/08/21/parsing-uri-query-strings-in-windows-8-metro-style-apps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.