从c#中的字符串末尾删除回车符和换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/873043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:59:31  来源:igfitidea点击:

Removing carriage return and new-line from the end of a string in c#

c#stringnewlinecarriage-return

提问by Avik

How do I remove the carriage return character (\r)and the new line character(\n)from the end of a string?

如何从字符串的末尾删除回车符(\r)和换行符(\n)

采纳答案by RichieHindle

This will trim off any combination of carriage returns and newlines from the end of s:

这将从 的末尾删除回车和换行符的任何组合s

s = s.TrimEnd(new char[] { '\r', '\n' });

Edit: Or as JP kindly points out, you can spell that more succinctly as:

编辑:或者正如 JP 指出的那样,您可以更简洁地拼写为:

s = s.TrimEnd('\r', '\n');

回答by JP Alioto

This should work ...

这应该工作...

var tst = "12345\n\n\r\n\r\r";
var res = tst.TrimEnd( '\r', '\n' );

回答by Matthew Flaschen

If there's always a single CRLF, then:

如果总是有一个 CRLF,那么:

myString = myString.Substring(0, myString.Length - 2);

If it may or may not have it, then:

如果它可能有也可能没有,那么:

Regex re = new Regex("\r\n$");
re.Replace(myString, "");

Both of these (by design), will remove at most a single CRLF. Cache the regex for performance.

这两个(按设计)将最多删除一个 CRLF。缓存正则表达式以提高性能。

回答by Crash893

String temp = s.replace("\r\n","").trim();

sbeing the original string.

s作为原始字符串。

回答by Simon

For us VBers:

对于我们 VBers:

TrimEnd(New Char() {ControlChars.Cr, ControlChars.Lf})

回答by vnRock

string k = "This is my\r\nugly string. I want\r\nto change this. Please \r\n help!";
k = System.Text.RegularExpressions.Regex.Replace(k, @"\r\n+", " ");

回答by Pat

varName.replace(/[\r\n]/mg, '')                                              

回答by user1585204

This was too easy -- for me I'm filtering out certain email items. I'm writing my own custom email junk filter. With \r and/or \n in the string it was wiping out all items instead of filtering.

这太简单了——对我来说,我正在过滤掉某些电子邮件项目。我正在编写自己的自定义电子邮件垃圾过滤器。使用 \r 和/或 \n 在字符串中,它会清除所有项目而不是过滤。

So, I just did filter = filter.Remove('\n') and filter = filter.Remove('\r'). I'm making my filter such that an end user can use Notepad to directly edit the file so there's no telling where these characters might embed themselves -- could be other than at the start or end of the string. So removing them all does it.

所以,我只是做了 filter = filter.Remove('\n') 和 filter = filter.Remove('\r')。我正在制作我的过滤器,以便最终用户可以使用记事本直接编辑文件,因此不知道这些字符可能嵌入在哪里——可能不是在字符串的开头或结尾。因此,将它们全部删除即可。

The other entries all work but Remove might be the easiest?

其他条目都有效,但 Remove 可能是最简单的?

I learned quite a bit more about Regex from this post -- pretty cool work with its use here.

我从这篇文章中学到了更多关于 Regex 的知识——在这里使用它非常酷。

回答by Alex Wiese

If you are using multiple platforms you are safer using this method.

如果您使用多个平台,则使用此方法会更安全。

value.TrimEnd(System.Environment.NewLine.ToCharArray());

It will account for different newline and carriage-return characters.

它将考虑不同的换行符和回车符。

回答by martinp999

s.TrimEnd();

The above is all I needed to remove '\r\n' from the end of my string.

以上是我从字符串末尾删除 '\r\n' 所需的全部内容。

The upvoted answer seems wrong to me. Firstly, it didn't work when I tried, secondly, if it did work I would expect that s.TrimEnd('\r', '\n') would only remove eithera '\r' or a '\n', so I'd have to run it over my string twice - once for when '\n' was at the end and the second time for when '\r' was at the end (now that the '\n' was removed).

赞成的答案对我来说似乎是错误的。首先,它不工作时,我试过了,其次,如果它没有工作,我会期望s.TrimEnd(“\ r”,“\ n”)将只删除或者一个“\ r”或“\ n” ,所以我必须在我的字符串上运行两次 - 一次是在 '\n' 结束时,第二次是在 '\r' 结束时(现在 '\n' 已被删除) .