How do I generate a Friendly URL in C#?
Asked Answered
D

5

29

How can I go about generating a Friendly URL in C#? Currently I simple replace spaces with an underscore, but how would I go about generating URL's like Stack Overflow?

For example how can I convert:

How do I generate a Friendly URL in C#?

Into

how-do-i-generate-a-friendly-url-in-C

Disquieting answered 1/9, 2008 at 11:55 Comment(0)
W
47

There are several things that could be improved in Jeff's solution, though.

if (String.IsNullOrEmpty(title)) return "";

IMHO, not the place to test this. If the function gets passed an empty string, something went seriously wrong anyway. Throw an error or don't react at all.

// remove any leading or trailing spaces left over
… muuuch later:
// remove trailing dash, if there is one

Twice the work. Considering that each operation creates a whole new string, this is bad, even if performance is not an issue.

// replace spaces with single dash
title = Regex.Replace(title, @"\s+", "-");
// if we end up with multiple dashes, collapse to single dash            
title = Regex.Replace(title, @"\-{2,}", "-");

Again, basically twice the work: First, use regex to replace multiple spaces at once. Then, use regex again to replace multiple dashes at once. Two expressions to parse, two automata to construct in memory, iterate twice over the string, create two strings: All these operations can be collapsed to a single one.

Off the top of my head, without any testing whatsoever, this would be an equivalent solution:

// make it all lower case
title = title.ToLower();
// remove entities
title = Regex.Replace(title, @"&\w+;", "");
// remove anything that is not letters, numbers, dash, or space
title = Regex.Replace(title, @"[^a-z0-9\-\s]", "");
// replace spaces
title = title.Replace(' ', '-');
// collapse dashes
title = Regex.Replace(title, @"-{2,}", "-");
// trim excessive dashes at the beginning
title = title.TrimStart(new [] {'-'});
// if it's too long, clip it
if (title.Length > 80)
    title = title.Substring(0, 79);
// remove trailing dashes
title = title.TrimEnd(new [] {'-'});
return title;

Notice that this method uses string functions instead of regex functions and char functions instead of string functions whenever possible.

Weintrob answered 1/9, 2008 at 12:35 Comment(2)
Great answer thanks, I also added URL = Regex.Replace(URL, @"<(.|\n)*?>", ""); to remove HTML tags as "my <b>article</b> here" will turn to "my-barticleb-here"Wally
A path also can't end with a period. Also not sure why you use new [] {'-'}, when you could just use '-'.Marsland
W
20

Here's how we do it. Note that there are probably more edge conditions than you realize at first glance..

if (String.IsNullOrEmpty(title)) return "";

// remove entities
title = Regex.Replace(title, @"&\w+;", "");
// remove anything that is not letters, numbers, dash, or space
title = Regex.Replace(title, @"[^A-Za-z0-9\-\s]", "");
// remove any leading or trailing spaces left over
title = title.Trim();
// replace spaces with single dash
title = Regex.Replace(title, @"\s+", "-");
// if we end up with multiple dashes, collapse to single dash            
title = Regex.Replace(title, @"\-{2,}", "-");
// make it all lower case
title = title.ToLower();
// if it's too long, clip it
if (title.Length > 80)
    title = title.Substring(0, 79);
// remove trailing dash, if there is one
if (title.EndsWith("-"))
    title = title.Substring(0, title.Length - 1);
return title;
Waterborne answered 1/9, 2008 at 12:3 Comment(1)
This answer is outdated. Jeff updates his original answer with a new version of the code: #25759Ra
U
6

This gets part of the way there (using a whitelist of valid characters):

new Regex("[^a-zA-Z-_]").Replace(s, "-")

It does, however, give you a string that ends with "--". So perhaps a second regex to trim those from the beginning/end of the string, and maybe replace any internal "--" to "-".

Unifoliolate answered 1/9, 2008 at 12:3 Comment(0)
K
1

here is a simple function which can convert your string to Url, you just need to pass title or string it will convert it to user friendly Url.

    public static string GenerateUrl(string Url)
    {
        string UrlPeplaceSpecialWords = Regex.Replace(Url, @"&quot;|['"",&?%\.!()@$^_+=*:#/\\-]", " ").Trim();
        string RemoveMutipleSpaces = Regex.Replace(UrlPeplaceSpecialWords, @"\s+", " ");
        string ReplaceDashes = RemoveMutipleSpaces.Replace(" ", "-");
        string DuplicateDashesRemove = ReplaceDashes.Replace("--", "-");
        return DuplicateDashesRemove.ToLower();
    }
Koala answered 13/11, 2019 at 10:40 Comment(0)
V
0

I made a small open source project to generate friendly urls (slugs) in C#, see: https://github.com/marthijn/Sidio.Text.Slugify or https://www.nuget.org/packages/Sidio.Text.Slugify/

var slugifier = Slugifier.Create();
var slug = slugifier.Slugify("Hello, World!");
Vibrissa answered 29/7 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.