views:

708

answers:

3

in C# I have a string I am writing to the outputstream of the response. After I save this document and open it in Notepad++ or WordPad I get nicely formatted line breaks where they are intended, when I open this document with the regular old windows notepad, I get one long text string with [] square looking symbols where the line breaks should be.

Has anyone had any experience with this?

Thanks

Jim

+7  A: 

Yes - it means you're using \n as the line break instead of \r\n. Notepad only understands the latter.

(Note that Environment.NewLine suggested by others is fine if you want the platform default - but if you're serving from Mono and definitely want \r\n, you should specify it explicitly.)

Jon Skeet
Thanks Jon. "serving from Mono"? What's that?
jim
As in, "Deploying a server using Mono on Linux, and asking that server to serve the data." Mono is a cross-platform implementation of .NET - see http://mono-project.com
Jon Skeet
+2  A: 

Try this :

string myStr = ...
myStr = myStr.Replace("\n", Environment.NewLine)
Guillaume
if input string contains both \n and \r\n such operation will break it
abatishchev
"I get one long text string with [] square looking symbols where the line breaks should be."Looks like it's not the case.
Guillaume
@Guillaume: It's not the case *this time*, but there's no reason why a file can't contain two or more different kinds of line separator--I see it in web pages all the time. When you're normalizing newlines you should always match all three kinds: `Regex.Replace(@"\n|\r\n?", "\r\n")`
Alan Moore
When you are noemalizing newlines from a non standard source ok.But when you KNOW you will ONLY have '\n' you don't need a regex...
Guillaume
+1  A: 

Use Environment.NewLine for line breaks.

Tadas