Created
June 27, 2016 12:03
-
-
Save metinsaylan/b6b8eca6df5c96155b18d51ce6f588cd to your computer and use it in GitHub Desktop.
String extension for GenerateSlug capability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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