Replace Line Breaks in a String C#

后端 未结 17 1105
失恋的感觉
失恋的感觉 2020-11-22 11:16

How can I replace Line Breaks within a string in C#?

17条回答
  •  野性不改
    2020-11-22 11:33

    If your code is supposed to run in different environments, I would consider using the Environment.NewLine constant, since it is specifically the newline used in the specific environment.

    line = line.Replace(Environment.NewLine, "newLineReplacement");
    

    However, if you get the text from a file originating on another system, this might not be the correct answer, and you should replace with whatever newline constant is used on the other system. It will typically be \n or \r\n.

提交回复
热议问题