3

I have the following code:

string test = @"1
2
3";

This line:

Console.WriteLine(test.Replace("\n", "")); //3

prints "3", instead of "123", which is what I expected. Why does this method return only the last line of my string, and how can I otherwise remove all new-lines?

Daniel
  • 2,944
  • 3
  • 22
  • 40

4 Answers4

8

Console.WriteLine(test.Replace("\n", "")); returns all numbers for me. When dealing with linebreaks you must be aware that newline may be \r\n, it depends on platform. In this case you should use this:

test.Replace("\r\n", "")
// or better:
test.Replace(Environment.NewLine, "")

Refrer to Difference between “\n” and Environment.NewLine

Community
  • 1
  • 1
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
  • 1
    Verbatium string does not use "\r\n" it uses the encoding of the source file while could be anything in theory. – Jeff Walker Code Ranger May 08 '13 at 15:35
  • @JeffWalkerCodeRanger you're wrong. All strings, by default use `UTF-16`, please refer to [this documentation page](http://msdn.microsoft.com/en-us/library/system.string.aspx#Characters). C# supports two forms of string literals: regular and verbatim. Both of which are using same encoding. – Zbigniew May 08 '13 at 16:07
  • @walkhard Yes, all C# strings are encoded with UTF-16. I was referring to the line ending encoding (i.e. whether they are Unix or PC line endings). That does "follow the encoding of the source file". So the suggested code is not a safe way to remove all line endings from the string. – Jeff Walker Code Ranger May 08 '13 at 19:43
2

On the off-chance that the newline might differ between the system on which you compose the text file and the system on which it is compiled or run, you might want to do

string test = @"1
2
3";
Console.WriteLine(test.Replace(@"
", "");

to do your best to ensure that the newline in your string literal is the same as the newline you're replacing.

Rawling
  • 49,248
  • 7
  • 89
  • 127
1

A verbatim string uses the line endings of the source code file it is in. Given that source control environments like git may convert line endings to the native line ending the string

string test = @"1
2
3";

could have any set of line endings ("\r\n","\r", or "\n"). The only safe way to remove any of those possible ones is:

Console.WriteLine(test.Replace("\n", "").Replace("\r", ""));

This makes no assumptions about the two code snippets being in the same source file (since two source files don't have to have the same line endings).

Jeff Walker Code Ranger
  • 4,634
  • 1
  • 43
  • 62
  • Got a reference for 'verbatim string uses the line endings of the source code file it is in'? Would be useful as I am seeing this behaviour - i.e. test passing on dev machine in VS, failing on TeamCity machine fetching from git. – Nick Baker May 10 '16 at 08:05
  • 1
    @NickBaker I suppose the language spec would say that, though I think it is unclear. I personally tested and verified that to be the case. – Jeff Walker Code Ranger May 14 '16 at 17:51
0

The verbatim string is using "\r\n" as the line break, so the result of your replace is "1\r2\r3".

If you instead use test.Replace("\r\n", "") it will work as expected. To handle the case of any number of \r or \n use: test.Replace("\r", "").Replace("\n", ""). That will work for \r, \n, or \r\n as a separator.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Verbatium string does not use "\r\n" it uses the encoding of the source file while could be anything in theory. – Jeff Walker Code Ranger May 08 '13 at 15:32
  • @JeffWalkerCodeRanger Yes, it could be anything in theory, but I was confidant that it would in fact be "\r\n" in this particular case, knowing that "\n" was tried and failed, as those are the two most common that are used. Additionally, the solution that I gave would work for any combination of "\n" and "\r". I am not aware of any system that uses characters beyond some combination of those two as a line break separator. – Servy May 08 '13 at 15:35