【问题标题】:How can I change a string to make it URL friendly? [duplicate]如何更改字符串以使其对 URL 友好? [复制]
【发布时间】:2011-10-14 22:00:51
【问题描述】:

可能重复:
URL Slugify alrogithm in C#?

我想像这样转换一个字符串:

Testing this on-the-test-forum (please/remove) if possible. test: test, "abc"

到这样的字符串:

testing-this-on-the-test-forum-please-remove-if-possible-test-test-abc

有没有人对我可以使用的最简单类型的函数有任何想法。我正在寻找的是非常简单的东西,因为我需要稍后在 LINQ 中使用这个函数。

【问题讨论】:

  • 这称为slug
  • 这开始看起来比我预期的要复杂得多:-(
  • @Marife 只需从副本中获取代码就可以了

标签: c#


【解决方案1】:

您最简单的方法似乎是在返回的字符串中识别您想要的字符并根据这些字符对其进行编码。所以,在伪代码中:

newstring = "";

foreach(character in newstring.tolower)
    if(character in allowedcharacters)
        newstring+=character;
    else
        newstring+="-";

return newstring.replace("--","-");

这应该是你的开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-14
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2010-12-25
    • 2012-02-22
    • 1970-01-01
    相关资源
    最近更新 更多