如何生成在C#中友好的URL? [英] How do I generate a Friendly URL in C#?

查看:197
本文介绍了如何生成在C#中友好的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何去用C#生成友好的URL?目前,我简单的用下划线代替空格,但我将如何去生成的URL像堆栈溢出?

例如我怎么能转换:


  

我如何生成在C#中友好的URL?


进入


  

怎么办,我生成 - 一个友好的URL,在-C



解决方案

有几件事情,可能会在杰夫的溶液,可提高,但。

 如果(String.IsNullOrEmpty(职称))返回;

IMHO,而不是测试此的地方。如果函数会得到一个空字符串,发生了严重的错误呢。抛出一个错误,或者根本不反应。

  //删除任何前导或尾随空格遗留
......后来muuuch:
//删除尾随破折号,如果有一个

两次工作。考虑到每个操作创建一个全新的串,这是不好的,即使表现是不是一个问题。

  //替换单破折号空间
标题= Regex.Replace(标题,@\\ S +, - );
//如果我们最终得到多个破折号,塌陷单破折号
标题= Regex.Replace(标题,@\\ - {2}, - );

再次基本上两次的工作:首先,使用正则表达式一次性替换多个空格。然后,再次使用正则表达式一次更换多个破折号。两位前pressions解析,两人自动机内存建设,在遍历字符串两次,创建两个字符串:所有这些操作都可以折叠到一个单一

关闭我的头顶,没有任何任何测试,这将是一个等效的解决方案:

  //使其全部小写
标题= title.ToLower();
//删除实体
标题= Regex.Replace(标题,@&放大器; \\ w +;,);
//删除任何不是字母,数字,破折号或空间
标题= Regex.Replace(标题,@[^ A-Z0-9 \\ - \\ S],);
//替换空格
标题= title.Replace('',' - ');
//崩溃破折号
标题= Regex.Replace(标题,@ - {2}, - );
//开头修剪过度破折号
标题= title.TrimStart(新[] {' - '});
//如果它太长,它夹
如果(title.Length> 80)
    标题= title.Substring(0,79);
//删除尾随破折号
标题= title.TrimEnd(新[] {' - '});
返回称号;

请注意,此方法使用字符串函数,而不是正则表达式的功能和char函数而不是字符串函数只要有可能。

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

解决方案

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.

这篇关于如何生成在C#中友好的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆