ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToUrlSlug

If you get Turkish inputs you can use this method to create url slugs

Source

public static string ToUrlSlug(this string text)
{
return Regex.Replace(
            Regex.Replace(
                Regex.Replace(
                    text.Trim().ToLower()
                            .Replace("ö", "o")
                            .Replace("ç", "c")
                            .Replace("ş", "s")
                            .Replace("ı", "i")
                            .Replace("ğ", "g")
                            .Replace("ü", "u"),
                @"\s+", " "), // multiple spaces to one space
                @"\s", "-"), // spaces to hypens
                @"[^a-z0-9\s-]", ""); // removing invalid chars
}

Example

var name = "Serdar Büyüktemiz çşğüİö";
var urlName = name.ToUrlSlug(); // returns serdar-buyuktemiz-csguio

Author: Serdar Büyüktemiz

Submitted on: 12 jan. 2013

Language: C#

Type: System.String

Views: 6000