6

I have a simple string which has '\n' in it. It is supposed to be written into a file. For eg:

var s = new StringBuilder();

s.Append("hello");
s.Append("\n");
s.Append("betty");
s.Append("is");
s.Append("my");
s.Append("\n");
s.Append("name");

string str = s.ToString();
Console.WriteLine(str);

This string prints as expected:

hello 
bettyismy
name

Now when I try to write this string in a file using the following:

var w = new StreamWriter(crcFilePath);
w.WriteLine(crcString);
w.Close();

The content of the file is:

hellobettyismyname

Any idea why it is ignoring the \n in the string.

user2695082
  • 285
  • 4
  • 16
  • 6
    use `Environment.NewLine` instead of `"\n"`. – MarcinJuraszek Apr 15 '14 at 05:50
  • 1
    How is `s` and `crcString` related? – Uwe Keim Apr 15 '14 at 05:50
  • Please take a look [this][1] this may be duplicate of this question. [1]: http://stackoverflow.com/questions/2617752/newline-character-in-stringbuilder – Dnyanesh Apr 15 '14 at 05:53
  • 2
    `crcString` obviously has more differences than you are showing us, for one thing the name is `ashna` in the second example and `betty` in the first. Please show a [SSCCE example](http://www.sscce.org/) – Scott Chamberlain Apr 15 '14 at 05:54
  • Actually this is just an example. The string is getting constructed in a C++ dll which has '\n' in it. – user2695082 Apr 15 '14 at 05:57
  • In windows, when file is written it need '\r\n' instead of just '\n' to move cursor to next line while file is being written. Thats why in your case '\n' is being ignored. For different OS, characters are different to go to next line, that why its advisable to use Environment.NewLine, it will return OS specific characters for new line. – Savaratkar Apr 15 '14 at 06:05

4 Answers4

7

It is probably the application or system you are using to view your file ignoring the line feed, not C# ignoring it. Instead of \n, try using System.Environment.NewLine or \r\n. See this question for info on \r and \n. Also, Notepad++ can show white space chars including new lines, which can be handy in such cases.

Community
  • 1
  • 1
xr280xr
  • 12,621
  • 7
  • 81
  • 125
7

The content of the file is:

 helloashnaismyname

The contents of the file is in fact this (in hex mode):

==========================================================================
      xDump Version 1.04 - Hex Dump Utility
      Copyright (c) Xidicone Pty Ltd 20013  -  All rights reserved.
==========================================================================
0000: 68 65 6C 6C 6F 0A 62 65   74 74 79 69 73 6D 79 0A | hello.bettyismy. 
0010: 6E 61 6D 65 0D 0A                                 | name..           
==========================================================================

Any idea why it is ignoring the \n in the string.

As can be seen by the hex dump, it is not ignoring the \n characters.

The problem is I suspect you opened the file in Notepad and it does not undestand the end of line markers and hence it displays it wrong.

Change the code to be this and Notepad will load the file as expected:

    var s = new StringBuilder();
                s.Append("hello");
                s.Append("\r\n");
                s.Append("betty");
                s.Append("is");
                s.Append("my");
                s.Append("\r\n");
                s.Append("name");
                string str = s.ToString();
jussij
  • 10,370
  • 1
  • 33
  • 49
2

You should use environment.newLine. The best method to do this is like:

s.Append(string.Format("Foo{0}Bar", Environment.NewLine));

The {0} will be replaced throughout a string by the second argument in the string.Format method (which in this case is a new line). It is a clean and recommended approach.

Cyassin
  • 1,437
  • 1
  • 15
  • 31
  • So you are transforming [someone else's comment](http://stackoverflow.com/questions/23075579/c-sharp-ignores-n-in-a-string-when-written-in-a-file#comment35265917_23075579) into an answer? – Uwe Keim Apr 15 '14 at 05:53
  • It is quite different actually. The end result is the same, but the best way to lay it out is how I have above. The comment you reference is just a straight swap, that would look ugly if thrown straight in the string. I dont see the comment as a great answer. – Cyassin Apr 15 '14 at 05:55
1

You can use s.AppendLine() instead of s.Append("\n"); Just have a try. StringBuilder.AppendLine() Method