So, my question is fairly simple. I have a string and I want to be able to use it in urls. Simple, right? The tricky part is, however, I want a custom way of encoding it. You see, my language is full of é, í, ô, ä, ľ,š,č,ť..., you get the idea.
So, let's say I have a string like this:
Čečenský bojovník sa pobil v košickej väzbe
If I use HttpUtility.EncodeUrl, I get this string:
%c4%8ce%c4%8densk%c3%bd+bojovn%c3%adk+sa+pobil+v+ko%c5%a1ickej+v%c3%a4zbe
However, my desired string would look like this (trying to have as user-friendly urls as possible):
cecensky-bojovnik-sa-pobil-v-kosickej-vazbe
Using the function EncodeUrl isn't an option then. So, I wrote myself a function to do multiple manipulations to the string, doing exactly what I need.
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;
}
I think it's obvious what's going on and it works perfectly fine. Well, except the fact a string is immutable, so there's quite a lot of unnecessary memory allocations going on.
So finally I got to my question - is there some recommended, more efficient way, of doing this?