Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top