如何从字符串中删除换行符?

216

I have a string in the following format

string s = "This is a Test String.\n   This is a next line.\t This is a tab.\n'

我想从上面的字符串中删除所有出现的\n\r

我尝试过string s = s.Trim(new char[] {'\n', '\r'});,但没有帮助。

12个回答

-4

如果你想要从开头和结尾删除内容,可以使用Trim

string stringWithoutNewLine = "\n\nHello\n\n".Trim();

-6

顺便提一下,

Trim() 已经实现了这个功能。

以下是 LINQPad 的示例:

void Main()
{
    var s = " \rsdsdsdsd\nsadasdasd\r\n ";
    s.Length.Dump();
    s.Trim().Length.Dump();
}

输出:

23
18

7
那并没有移除中间的换行符\n。 - FlappySocks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接