Replace Line Breaks in a String C#

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

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

17条回答
  •  無奈伤痛
    2020-11-22 11:29

    I would use Environment.Newline when I wanted to insert a newline for a string, but not to remove all newlines from a string.

    Depending on your platform you can have different types of newlines, but even inside the same platform often different types of newlines are used. In particular when dealing with file formats and protocols.

    string ReplaceNewlines(string blockOfText, string replaceWith)
    {
        return blockOfText.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith);
    }
    

提交回复
热议问题