English 中文(简体)
C# 执行字符串自定义编码的推荐方式
原标题:C# Recommended way of performing a custom encoding of a string

所以,我的问题很简单。我有一个字符串,我想用它来做骨灰圈。简单,对吗?棘手的部分是,但是,我想用一种定制的方法来编码它。你看,我的语言充满了 ——,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 你明白我的意思。

这么说吧,我有这样一条绳子:

Čečenský bojovník sa pobil v košickej väzbe

如果我使用 HttpUlity. EncodUrl, 我就会得到这个字符串 :

%c4%8ce%c4%8densk%c3%bd+bojovn%c3%adk+sa+pobil+v+ko%c5%a1ickej+v%c3%a4zbe

不过,我所希望的绳子看起来是这样的(试图尽可能有方便用户的):

cecensky-bojovnik-sa-pobil-v-kosickej-vazbe

使用 EncodeUrl 函数不是一个选项。 因此, 我给自己写了一个函数, 要对字符串进行多次操作, 完全按照我的要求做 。

public static string EncodeForUrl(this string s)
{
    string temp = s.StripDiacritics();  // one custom function
    temp = temp.ToLower();
    temp = temp.Trim();
    temp = temp.Replace(" ", "-");
    return temp;
}

我认为,这很明显地说明了发生了什么事情,而且效果很好。嗯,除了一个字符串是不可改变的,所以有很多不必要的内存分配在进行中。

所以最后我问了一下我的问题, 是否有一些建议, 更有效率的方法, 这样做?

最佳回答

我终于找到一个能满足我需要的答案。

Stack overflow如何生成其SEO友好的URL?

这是用来剥除diacritics, 甚至比我现在的版本更好

https://meta.stackschange.com/ queses/74335/non-us-ascii-characters-droped-from-full-proflem-url/7696#7696

问题回答

您可以跳过ToLower () 而不是使用 替换 () 您可以做类似的事情 : < a href="https://stackoverflow.com/a/5203674/730701" >https://stackoverflow.com/a/5203674/737001





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签