将特定字符文化化为良好的 URL 格式

发布于 2024-08-28 09:46:24 字数 153 浏览 5 评论 0原文

我需要一些功能来以 url 友好的格式制作以下字符串: “knæ som gør”应为“kna-som-gor”

,即将文化特定字符替换为可在 url 中使用的字符。

使用 .Net 和 C#

请帮助我:)

/Andreas

I need some functionality to make the following string in a url-friendly format:
"knæ som gør" should be "kna-som-gor"

That is, replacing culture specific characters to characters that can be used in urls.

Using .Net and C#

Please help me :)

/Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风苍溪 2024-09-04 09:46:24

不要让事情复杂化。 :)

使用正则表达式,或者简单地使用 String.Replace< /a>.

Don't complicate things. :)

Either use a regexp, or simply use String.Replace.

ㄖ落Θ余辉 2024-09-04 09:46:24

您可以在此处找到删除变音符号的解决方案:如何从 .NET 中的字符串中删除变音符号(重音符号)?。不过,这个解决方案对 æ 或 ø 没有帮助。

也许这会删除足够多的特殊字符,以便可以使用简单的替换来翻译其余字符?

如果“url-Friendly”并不意味着漂亮,您还可以使用 HttpUtility.UrlEncode,它会生成
“kn%c3%a6+som+g%c3%b8r”。

You can find a solution that removes diacritics here: How do I remove diacritics (accents) from a string in .NET?. This solution does not help you with æ or ø, though.

Maybe that removes enough of your special characters that the rest can be translated using simple replacing?

If "url-friendly" does not mean pretty, you could also use HttpUtility.UrlEncode, which produces
"kn%c3%a6+som+g%c3%b8r".

有深☉意 2024-09-04 09:46:24

编辑:添加了可能的解决方案(帖子结束)。

我有一个非常相似的问题,尽管是文件名而不是 URL。主要问题似乎是没有标准方法来请求“ø 的最佳 ASCII 替换”,因此即使您可以找到所有不需要的字符,也很难自动插入哪个替换。

我发布了很多可能有用的代码。有关详细信息,请参阅此 StackOverflow 问题

编辑:我认为这个问题的解决方案在于 StringInfo,它允许您迭代字符串中的子字符(Unicode 代理或组合字符)。这应该可以检测和转换类似 å 的内容(可以在 Unicode 中编码为 A-WITH-RING 或 RINGED-A;过滤掉装饰器并保留普通字符的部分)。

Edit: Added possible solution (end of post).

I had a very similar problem, albeit for file names rather than URLs. The main problem seems to be that there is no standard way to ask for the "best ASCII replacement for ø", so even if you can locate all the unwanted characters it is hard to automate which replacement to insert.

I posted quite a bit of code that might be helpful. See this StackOverflow question for details.

Edit: I think the solution to this problem lies with StringInfo, which allows you to iterate through the sub-characters (Unicode surrogates or combining characters) in a string. This should make it possible to detect and convert something like å (which can be encoded in Unicode as either A-WITH-RING or RINGED-A; filter out the decorator and keep the part that is a normal character).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文