Replace characters in a string to fit browser, C#, From UTF8 encoding

Tag: string , browser , utf-8 Author: jjxf8848 Date: 2011-09-05

I need to replace the characters å, ä, ö to browser friendly chars. For example ä should become %E4.

I tried weirdString = Uri.EscapeUriString(weirdString); But it doesnt convert the åäö to the right sign. Help please?

Edit: Tried this:

ASCIIEncoding ascii = new ASCIIEncoding(); 
byte[] asciicharacters = Encoding.UTF8.GetBytes("vägen");
byte[] asciiArray = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, asciicharacters);
string finalString = ascii.GetString(asciiArray);
string fixedAddrString = HttpUtility.HtmlEncode(finalString);
"å" is a perfectly browser friendly character. What are you trying to do/what's the problem you're encountering?
For instance, I need "dadelvägen" to become "dadelv%E4gen". Otherwise I get the wrong HTML-page.
Do you mean in the URL or in the HTML?
I meant for URL.
What are you getting instead of the expected values?

Other Answer1

If you need the characters to display on the page and not part of a URL, you should use Server.HtmlEncode.

var encodedString = Server.HtmlEncode(myString); 

HTML encoding makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML. For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as the opening or closing bracket of an HTML tag. When the characters are HTML encoded, they are converted to the strings < and >, which causes the browser to display the less than sign and greater than sign correctly.


Since you are using UTF-8, these characters are escaped to UTF-8, not ASCII.

You need to convert the string from UTF-8 to ASCII, using the Encoding classes before you try to escape them. That is, if you do want the ASCII values to come up.

See here.


It was meant to be used in the URL.
@mdc - You will need to convert the string to an ASCII string before encoding.
Cant get it right with converting it to ASCII, whats wrong here: (See OP, pasted the code there). The final string only becomes "v?gen" Not "v%E4gen".
@mdc - Have you checked the result of the encoding?
Yes I get a questionmark for the "ä" for some reason, probably doing it wrong. I would really appreciate if you could experiment with that letter ("ä") for a couple a minute to see if you can make it "%E4". Its driving me mad now =).

Other Answer2

Try HttpUtility.HtmlDecode. If this doesent working too you can do a simple string.Replace for this chars.


HtmlDecode? Are you sure?
I think he meant HtmlEncode, I tried that but the resulting string for "vägen" becomes "v&#228;gen", it should be "v%E4gen".
Its what I used with HtmlEncode. How can I get the correct string?