[フレーム]
Last Updated: December 19, 2019
·
9.341K
· joancaron

How to generate clean url slug in C#

public static string ToUrlSlug(string value){ 

 //First to lower case 
 value = value.ToLowerInvariant(); 

 //Remove all accents
 var bytes = Encoding.GetEncoding("Cyrillic").GetBytes(value); 

 value = Encoding.ASCII.GetString(bytes); 

 //Replace spaces 
 value = Regex.Replace(value, @"\s", "-", RegexOptions.Compiled); 

 //Remove invalid chars 
 value = Regex.Replace(value, @"[^\w\s\p{Pd}]", "",RegexOptions.Compiled); 

 //Trim dashes from end 
 value = value.Trim('-', '_'); 

 //Replace double occurences of - or \_ 
 value = Regex.Replace(value, @"([-_]){2,}", "1ドル", RegexOptions.Compiled); 

 return value ; 
}

AltStyle によって変換されたページ (->オリジナル) /