Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- public class SlugGenerator
- {
- public static string GenerateSlug(string title)
- {
- if (string.IsNullOrWhiteSpace(title))
- return string.Empty;
- // Convert to lowercase
- title = title.ToLowerInvariant();
- // Replace whitespaces with dashes
- title = Regex.Replace(title, @"\s+", "-");
- // Remove invalid characters
- title = Regex.Replace(title, @"[^a-z0-9\-]", "");
- // Replace multiple dashes with a single dash
- title = Regex.Replace(title, @"-+", "-");
- // Trim dashes from the ends
- title = title.Trim('-');
- return title;
- }
- public static void Main()
- {
- string productTitle = Console.ReadLine();
- string slug = GenerateSlug(productTitle);
- Console.WriteLine($"Slug: {slug}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment