移动所有行,直到找到空行

时间:2013-04-16 00:19:06

标签: c# .net regex

我想将所有行从String apa1;移到String apa2;,直到找到第一个空白行。


apa1可能如下所示:

texttest: text test
test text test text

Lorem ipsum dolor sit
amet, consectetur 
adipiscing: elit. 
Curabitur dolor lectus, 
cursus ac placerat vitae, 
volutpat sit: amet lacus.

我希望apa1看起来像这样:

Lorem ipsum dolor sit
amet, consectetur 
adipiscing: elit. 
Curabitur dolor lectus, 
cursus ac placerat vitae, 
volutpat sit: amet lacus.

和apa2看起来像这样:

texttest: text test
test text test text

你如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

它应该是这样的。只需将第一个字符串复制到第二个字符串,直到您点击一个空行

String apa1 = @"texttest: text test
                test text test text

                Lorem ipsum dolor sit
                amet, consectetur 
                adipiscing: elit. 
                Curabitur dolor lectus, 
                cursus ac placerat vitae, 
                volutpat sit: amet lacus.";
String apa2 = string.Empty;

String tmp = String.Copy(apa1);
StringReader strReader = new StringReader(tmp );
String line = strReader.ReadLine();

while(!string.IsNullOrEmpty(line))
{
    apa2 += line;
    line = strReader.ReadLine();
}

apa1 = strReader.ReadToEnd();