Thursday, November 19, 2009

Generating meaningful names from a url


A common programming task is to extract some meaningful name from a url.
One example is saving the contents of a url to a file name on disk - we need to have the name for the file.
Bellow there is a c# method that I have written that extracts this name + the results it produces on a few urls. - Sanity - Sanity - Sanity1234567890123456789012345678901234567 - Sanity - Sanity - Sanity - Sanity_wsdl - Sanity

public static string GetSuggestedNameFromUrl(string url, string defaultValue)
  const int MaxChars = 50;

  string res = Path.GetFileNameWithoutExtension(url);

  //check if there is no file name, i.e. just folder name + query string
  if (String.IsNullOrEmpty(res) || IsNameOnlyQueryString(res))
    res = Path.GetDirectoryName(url);
    res = Path.GetFileName(res);

    if (String.IsNullOrEmpty(res))
       res = defaultValue;

  res = ReplaceInvalidCharacters(res);

  if (res.Length > MaxChars)
     res = res.Substring(0, MaxChars);

  return res;

private static string ReplaceInvalidCharacters(string res)
  return Regex.Replace(res, @"[^\w]", "_", RegexOptions.Singleline);

private static bool IsNameOnlyQueryString(string res)
  return !String.IsNullOrEmpty(res) && res[0]=='?';


What's next? get this blog rss updates or register for mail updates!