从 C# 中的字符串末尾删除回车符和换行符
如何删除回车符 (\r)
和 字符串末尾的 Unix 换行符(\n)
?
How do I remove the carriage return character (\r)
and
the Unix newline character(\n)
from the end of a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这太简单了——对我来说,我正在过滤掉某些电子邮件项目。 我正在编写自己的自定义电子邮件垃圾过滤器。 在字符串中使用 \r 和/或 \n 时,它会清除所有项目而不是过滤。
所以,我只是做了filter = filter.Remove('\n') 和filter = filter.Remove('\r')。 我正在制作过滤器,以便最终用户可以使用记事本直接编辑文件,因此无法知道这些字符可能嵌入到何处 - 可能位于字符串的开头或结尾以外的位置。 所以把它们全部删除就可以了。
其他条目都可以工作,但“删除”可能是最简单的?
我从这篇文章中学到了更多关于正则表达式的知识——在这里使用它非常酷。
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.
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.
The other entries all work but Remove might be the easiest?
I learned quite a bit more about Regex from this post -- pretty cool work with its use here.
我使用了很多擦除级别
,现在我们有了干净的一行
I use lots of erasing level
and now we have a clean one row
这就是我必须为我工作的。
This is what I got to work for me.
如果您使用 nuget IvanStoychev.Useful.String.Extensions 您可以这样做:
s = s.TrimEnd(Environment.Newline);
它添加了一个带有
string
的TrimEnd
重载。If you use the nuget IvanStoychev.Useful.String.Extensions you can do:
s = s.TrimEnd(Environment.Newline);
It adds an overload of
TrimEnd
that takes astring
.这将从
s
末尾删除任何回车符和换行符的组合:编辑:或者正如 JP 善意指出的那样,您可以将其拼写为更简洁:
This will trim off any combination of carriage returns and newlines from the end of
s
:Edit: Or as JP kindly points out, you can spell that more succinctly as:
这应该有效...
This should work ...
s
是原始字符串。 (注意大写)s
being the original string. (Note capitals)如果您使用多个平台,那么使用此方法会更安全。
它将考虑不同的换行符和回车符。
If you are using multiple platforms you are safer using this method.
It will account for different newline and carriage-return characters.
以上是我从字符串末尾删除 '\r\n' 所需的全部内容。
赞成的答案对我来说似乎是错误的。 首先,当我尝试时它不起作用,其次,如果它起作用,我希望 s.TrimEnd('\r', '\n') 只会删除任一一个 '\r ' 或 '\n',所以我必须在字符串上运行它两次 - 一次是当 '\n' 位于末尾时,第二次是当 '\r' 位于末尾时(现在'\n' 被删除)。
The above is all I needed to remove '\r\n' from the end of my string.
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 either a '\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).
如果总是有一个 CRLF,那么:
如果它可能有也可能没有,那么:
这两者(根据设计)将最多删除一个 CRLF。 缓存正则表达式以提高性能。
If there's always a single CRLF, then:
If it may or may not have it, then:
Both of these (by design), will remove at most a single CRLF. Cache the regex for performance.
对于我们VBers来说:
For us VBers: