我有以下字符串。从C#中删除新行字符字符串
string str = @"One
Two
Four
Five
Six
Seven
Eight
Thirteen
Twenty
";
我想删除此字符串中多余的新行。所以该字符串应该看起来像:
str = "One
Two
Four
Five
Six
Seven
Eight
Thirteen
Twenty"
我正在使用此代码,但它不起作用。
Str = Str.Replace("\n\n", "\n");
while (Str.IndexOf("\n") > 0)
{
Str = Str.Replace("\n\n", "\n");
}
我甚至尝试过Str = Str.Replace("\u000a\u000a", "\u000a");
但仍然没有解决。
我现在还不确定,是第一个参数regex?如果是的话,这应该工作'Str.Replace(“[\ n] +”,“\ n”);'或者可能包括空格:'Str.Replace(“[] * [\ n] + [] *” ,“\ n”);' – libik 2014-10-29 16:16:49