Posted By

AndrewVos on 05/14/08


Tagged

escape search google query


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

AndrewVos
carlosabargues
leonbda
umang_nine
madelk


Launch Google Search in default Browser


 / Published in: C#
 

Properly escapes the query string, according to http://code.google.com/apis/searchappliance/documentation/46/xmlreference.html#appendixurl_escaping

  1. /// <summary>
  2. /// Class to launch Google Search in default browser.
  3. /// </summary>
  4. public static class Google {
  5. /// <summary>
  6. /// Launches Google Search in default browser, and escapes string according to: http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#appendix_url_escaping
  7. /// </summary>
  8. /// <param name="searchQuery">The search query.</param>
  9. public static void SearchGoogle(string searchQuery) {
  10. string fixedSearchQuery = null;
  11.  
  12. foreach (char character in searchQuery) {
  13. if (Char.IsLetterOrDigit(character)) {
  14. fixedSearchQuery += character;
  15. } else if (character == Char.Parse(" ")) {
  16. fixedSearchQuery += "+";
  17. } else {
  18. fixedSearchQuery += Uri.HexEscape(character);
  19. }
  20. }
  21.  
  22. string url = @"http://www.google.com/search?hl=en&q=" + fixedSearchQuery;
  23.  
  24. try {
  25. Process.Start(url);
  26. } catch { }
  27. }
  28. }

Report this snippet  

You need to login to post a comment.