Skip to content

Instantly share code, notes, and snippets.

@metinsaylan
Created June 27, 2016 12:03
Show Gist options
  • Save metinsaylan/b6b8eca6df5c96155b18d51ce6f588cd to your computer and use it in GitHub Desktop.
Save metinsaylan/b6b8eca6df5c96155b18d51ce6f588cd to your computer and use it in GitHub Desktop.
String extension for GenerateSlug capability
<Extension()>
Public Function GenerateSlug(ByVal this As String)
Dim str As String = this.RemoveAccent().ToLower()
' invalid chars
str = Regex.Replace(str, "[^a-z0-9\s-]", "", RegexOptions.IgnoreCase)
' convert multiple spaces into one space
str = Regex.Replace(str, "\s+", " ", RegexOptions.IgnoreCase).Trim()
' cut And trim
str = str.Substring(0, IIf(str.Length <= 45, str.Length, 45)).Trim()
str = Regex.Replace(str, "\s", "-", RegexOptions.IgnoreCase) ' hyphens
Return str
End Function
<Extension()>
Public Function RemoveAccent(ByVal this As String)
Dim bytes As Byte() = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(this)
Return System.Text.Encoding.ASCII.GetString(bytes)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment