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

发布于 2024-07-20 17:25:03 字数 69 浏览 6 评论 0原文

如何删除回车符 (\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

铁憨憨 2024-07-27 17:25:04
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+", " ");
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+", " ");
微凉徒眸意 2024-07-27 17:25:04

这太简单了——对我来说,我正在过滤掉某些电子邮件项目。 我正在编写自己的自定义电子邮件垃圾过滤器。 在字符串中使用 \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.

云之铃。 2024-07-27 17:25:04

我使用了很多擦除级别

String donen = "lots of stupid whitespaces and new lines and others..."

//Remove multilines
donen = Regex.Replace(donen, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
//remove multi whitespaces
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
donen = regex.Replace(donen, " ");
//remove tabs
char tab = '\u0009';
donen = donen.Replace(tab.ToString(), "");
//remove endoffile newlines
donen = donen.TrimEnd('\r', '\n');
//to be sure erase new lines again from another perspective
donen.Replace(Environment.NewLine, "");

,现在我们有了干净的一行

I use lots of erasing level

String donen = "lots of stupid whitespaces and new lines and others..."

//Remove multilines
donen = Regex.Replace(donen, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
//remove multi whitespaces
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
donen = regex.Replace(donen, " ");
//remove tabs
char tab = '\u0009';
donen = donen.Replace(tab.ToString(), "");
//remove endoffile newlines
donen = donen.TrimEnd('\r', '\n');
//to be sure erase new lines again from another perspective
donen.Replace(Environment.NewLine, "");

and now we have a clean one row

为你拒绝所有暧昧 2024-07-27 17:25:04

这就是我必须为我工作的。

s.Replace("\r","").Replace("\n","")

This is what I got to work for me.

s.Replace("\r","").Replace("\n","")
说谎友 2024-07-27 17:25:04
varName.replace(/[\r\n]/mg, '')                                              
varName.replace(/[\r\n]/mg, '')                                              
谎言月老 2024-07-27 17:25:04

如果您使用 nuget IvanStoychev.Useful.String.Extensions 您可以这样做:

s = s.TrimEnd(Environment.Newline);

它添加了一个带有 stringTrimEnd 重载。

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 a string.

自由范儿 2024-07-27 17:25:03

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

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

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

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

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

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

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

s = s.TrimEnd('\r', '\n');
我不吻晚风 2024-07-27 17:25:03

这应该有效...

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

This should work ...

var tst = "12345\n\n\r\n\r\r";
var res = tst.TrimEnd( '\r', '\n' );
羁绊已千年 2024-07-27 17:25:03
String temp = s.Replace("\r\n","").Trim();

s 是原始字符串。 (注意大写)

String temp = s.Replace("\r\n","").Trim();

s being the original string. (Note capitals)

霞映澄塘 2024-07-27 17:25:03

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

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

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

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.

笑饮青盏花 2024-07-27 17:25:03
s.TrimEnd();

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

赞成的答案对我来说似乎是错误的。 首先,当我尝试时它不起作用,其次,如果它起作用,我希望 s.TrimEnd('\r', '\n') 只会删除任一一个 '\r ' 或 '\n',所以我必须在字符串上运行它两次 - 一次是当 '\n' 位于末尾时,第二次是当 '\r' 位于末尾时(现在'\n' 被删除)。

s.TrimEnd();

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).

吻安 2024-07-27 17:25:03

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

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

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

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

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

If there's always a single CRLF, then:

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.

ゝ杯具 2024-07-27 17:25:03

对于我们VBers来说:

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

For us VBers:

TrimEnd(New Char() {ControlChars.Cr, ControlChars.Lf})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文