A: 

The iconv function converts a string between the specified encodings. In .NET, strings are always in UTF-16 internally, so it doesn't make sense to convert it. It's only when you write the string to a stream that you have to worry about the encoding. So you can ignore this part of the code... Just make sure you use the correct encoding when you write the resulting URL (you can specify the encoding when you create a StreamWriter, for instance)

Thomas Levesque
updated my question. the primary purpose was to tranlate the non-ASCII characters to its ASCII equivalent.
rockacola
+1  A: 

conversion to string:

byte[] unicodeBytes = Encoding.Unicode.GetBytes(str);
byte[] asciiBytes = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, unicodeBytes);
string asciiString = Encoding.ASCII.GetString(asciiBytes);

conversion to bytes:

byte[] ascii = Encoding.ASCII.GetBytes(str);

@Thomas Levesque is right, will get encoded by the output stream...

to remove the diacritics (accent marks), you can use the String.Normalize function, as detailed here:

http://blogs.msdn.com/michkap/archive/2007/05/14/2629747.aspx

that should take care of most of the cases (where the glyph is really a character plus an accent mark). for an even more aggressive char matching (to take care of cases like the Scandinavian slashed o [Ø], digraphs, and other exotic glyphs), there's the table approach:

http://www.codeproject.com/KB/cs/UnicodeNormalization.aspx

this includes around 1,000 symbol mappings in addition to the normalization.

(note, all punctuation is removed by the regex replace in your example)

+5  A: 

I would also like to add that the //TRANSLIT removes the apostrophes and that @jxac solution doesn't address that. I'm not sure why but by first encoding it to Cyrillic and then to ASCII you get a similar behavior as //TRANSLIT.

var str = "éåäöíØ";
var noApostrophes = Encoding.ASCII.GetString(Encoding.GetEncoding("Cyrillic").GetBytes(str)); 

=> "eaaoiO"
Jonas Elfström
Thanks so much for this solution! I have been looking for a way to replace non-US-ASCII characters with an ASCII equivalent for an old mainframe system that can't handle these characters.
Coderuckus
+1  A: 

There is a .NET library for transliteration on codeplex - unidecode (http://unidecode.codeplex.com/). It generally does the trick using Unidecode tables ported from python.

ikutsin