ASP.Net: Textarea carriage returns in IE and Firefox

Today I came across a problem with carriage returns in a textarea and came up with a solution.

Basically what’s happening is we are generating a PDF with an address in it that the user has the option of using the one supplied by the database or if it doesn’t exist then the user enters the address in a text area.

The address might be something like

119 Farringdon Road
United Kingdom

Now we had the following code:

address =  address.Replace(Environment.NewLine, "\n")

This string then gets passed to the PDF generator.

Now when you submit this from IE it works fine but when you submit from Firefox the address ends up like this

119 Farringdon RoadLondonEC1R 3DAUnited Kingdom


address  = address.Replace(Chr(13), "\n").Replace(Chr(10), "\n")
address =  address.Replace("\n\n", "\n")

The first line replaces chr(13) and chr(10) with “\n” as it seems that IE and firefox uses different carrige return characters. When I tested this in IE the address ended up with double carriage returns. Thus the reason for the second line which finds “\n\n” and replaces it with “\n”

Hope this helps somebody