0

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);
Oded
  • 489,969
  • 99
  • 883
  • 1,009
mdc
  • 1,161
  • 6
  • 22
  • 37

2 Answers2

1

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.


Update:

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.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • It was meant to be used in the URL. – mdc Sep 21 '11 at 10:31
  • @mdc - You will need to convert the string to an ASCII string before encoding. – Oded Sep 21 '11 at 10:42
  • 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 Sep 21 '11 at 12:04
  • @mdc - Have you checked the result of the encoding? – Oded Sep 21 '11 at 12:18
  • 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 =). – mdc Sep 21 '11 at 12:37
  • @mdc - Try using `UnicodeEncoding` instead of `UTF8` (only available in .NET 4.0). – Oded Sep 21 '11 at 12:43
  • How are you getting the original string? Are you certain it is UTF-8? – Oded Sep 21 '11 at 12:54
  • Yes its UTF-8, but I had to give up and move on. It took too much time. I just did an ugly hack with Strings Replace method to replace the characters. – mdc Sep 23 '11 at 13:14
0

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

NKnusperer
  • 974
  • 1
  • 11
  • 31
  • I think he meant HtmlEncode, I tried that but the resulting string for "vägen" becomes "vägen", it should be "v%E4gen". – mdc Sep 21 '11 at 10:08
  • Its what I used with HtmlEncode. How can I get the correct string? – mdc Sep 21 '11 at 10:26