views:

169

answers:

1

I have created a function which will convert any string into tab delimited.

What's new in ASP.NET 4.0

Then it will convert above title to following:

what-s-new-in-asp-net-4-0

I am using this to make my URL's SEO'd. But I am not sure that it will work fine in all cases or not. Till now I have tested this function on around 1000 records in my database and its working fine for all titles. Guyz please check this function and let me know if there is possibility of failing this function, and if there is a possibility that this function may fail, then please tell me the correct function which I can use in my app.

public string SEO_makeTitle(object objTitle)
    {
        string strTitle = Convert.ToString(objTitle);

        strTitle = Regex.Replace(strTitle.Trim(), @"\W", " "); //replace special chars
        strTitle = Regex.Replace(strTitle.Trim(), @"\s{2,}", " "); //replace double space
        strTitle = strTitle.Trim().Replace(" ", "-").ToLower();

        return strTitle; //return - delimited title
    }

Thanks

+5  A: 

You might want to consider what accents will look like. You're replacing "special" characters, but I doubt that that includes non-ASCII letters.

I would try to convert accented characters to non-accented ones first. There's a relatively easy way to do this in C# if you know the trick:

 static string RemoveAccents (string input) 
 { 
     string normalized = input.Normalize(NormalizationForm.FormKD); 
     Encoding removal = Encoding.GetEncoding 
         (Encoding.ASCII.CodePage, 
          new EncoderReplacementFallback(""), 
          new DecoderReplacementFallback("")); 
     byte[] bytes = removal.GetBytes(normalized); 
     return Encoding.ASCII.GetString(bytes); 
 }

You might also want to explicitly use ToLower(CultureInfo.InvariantCulture) to avoid problems if you run the code in Turkey. This probably wouldn't be a problem if you run the ToLower before running RemoveAccents admittedly.

Jon Skeet
Ya, I suspect that it may fail in case of special chars for different languages. like chines, hindi, arabic etc.....But if I use only english URL's (ASCII) then I think ma function will work fine...
Prashant
Even words used in English sometimes have accents on. (Café for example.) Oh, and you may want to consider the possibility that two entries will be mapped to the same URL - definitely include some sort of unique identifier in the URL too (like SO does).
Jon Skeet
That means I have to consider accents, Ok. Ya I am making URL's like SO, http://example.com/company/810345/company-name-in-california
Prashant