从字符串C#中删除空行

时间:2016-07-11 08:56:32

标签: c# string replace

如何从字符串中删除空行,例如:

转过来:

enter image description here

到此:

enter image description here

我使用了这段代码text.Replace("\n","");,但它转为:

testtesttes

2 个答案:

答案 0 :(得分:3)

    string data = stringData.Replace("\r\n\r\n", "\r\n"); 

答案 1 :(得分:2)

您应该替换两个换行符(如果有)。以下代码适用于Unix以及非unix平台。

var stringWithoutEmptyLines = yourString.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
相关问题