在 C# 中从字符串中修剪换行符的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038031/
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
What is the easiest way in C# to trim a newline off of a string?
提问by Edward Tanguay
I want to make sure that _content does not end with a NewLine character:
我想确保 _content 不以 NewLine 字符结尾:
_content = sb.ToString().Trim(new char[] { Environment.NewLine });
but the above code doesn't worksince Trim seems to not have an overloaded parameter for a collection of strings, only characters.
但是上面的代码不起作用,因为 Trim 似乎没有用于字符串集合的重载参数,只有字符。
What is the simplest one-liner to remove an Enivronment.Newline from the end of a string?
从字符串末尾删除 Enivronment.Newline 的最简单的单行是什么?
采纳答案by Simon Wilson
The following works for me.
以下对我有用。
sb.ToString().TrimEnd( '\r', '\n' );
or
或者
sb.ToString().TrimEnd( Environment.NewLine.ToCharArray());
回答by heavyd
What about
关于什么
_content = sb.ToString().Trim(Environment.NewLine.ToCharArray());
回答by Jon Skeet
How about:
怎么样:
public static string TrimNewLines(string text)
{
while (text.EndsWith(Environment.NewLine))
{
text = text.Substring(0, text.Length - Environment.NewLine.Length);
}
return text;
}
It's somewhat inefficient if there are multiple newlines, but it'll work.
如果有多个换行符,效率会有点低,但它会起作用。
Alternatively, if you don't mind it trimming (say) "\r\r\r\r"
or "\n\n\n\n"
rather than just "\r\n\r\n\r\n"
:
或者,如果您不介意修剪(例如)"\r\r\r\r"
或"\n\n\n\n"
不仅仅是"\r\n\r\n\r\n"
:
// No need to create a new array each time
private static readonly char[] NewLineChars = Environment.NewLine.ToCharArray();
public static string TrimNewLines(string text)
{
return text.TrimEnd(NewLineChars);
}
回答by bang
_content = sb.TrimEnd(Environment.NewLine.ToCharArray());
This will of course remove "\r\r\r\r" as well as "\n\n\n\n" and other combinations. And in "enviroments" where NewLine is other than "\n\r" you might get some strange behaviors :-)
这当然会删除 "\r\r\r\r" 以及 "\n\n\n\n" 和其他组合。在 NewLine 不是 "\n\r" 的“环境”中,您可能会遇到一些奇怪的行为:-)
But if you can live with this then I belive this is the most effectiv way to remove new line characters at the end of a string.
但是,如果您能接受这个,那么我相信这是删除字符串末尾换行符的最有效方法。
回答by Scott Weinstein
Use the Framework. The ReadLine() method has the following to say:
使用框架。ReadLine() 方法有以下内容:
A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r") or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed.
一行被定义为一个字符序列,后跟换行符(“\n”)、回车符(“\r”)或回车紧跟换行符(“\r\n”)。返回的字符串不包含终止回车符或换行符。
So the following will do the trick
所以以下将解决问题
_content = new StringReader(sb.ToString()).ReadLine();
回答by Richard Dunlap
How about just:
怎么样只是:
string text = sb.ToString().TrimEnd(null)
That will pull all whitespace characters from the end of the string -- only a problem if you wanted to preserve non-newline whitespace.
这将从字符串的末尾拉出所有空格字符 - 如果您想保留非换行符空格,这只是一个问题。
回答by Brian
Somewhat of a non-answer, but the easiest way to trim a newline off of a string is to not have the newline on the string in the first place, by making sure it is is never seen by your own code. That is, by using native functions which remove the newline. Many stream and file/io methods will not include the newline if you ask for output line by line, though it may be necessary to wrap something in a System.IO.BufferedStream
.
有点没有答案,但从字符串中删除换行符的最简单方法是首先不要在字符串上放置换行符,确保您自己的代码永远不会看到它。也就是说,通过使用删除换行符的本机函数。如果您逐行要求输出,则许多流和文件/io 方法将不包含换行符,尽管可能需要将某些内容包装在System.IO.BufferedStream
.
Things like System.IO.File.ReadAllLines
can be used in place of System.IO.File.ReadAllText
most of the time, and ReadLine
can be used instead of Read
once you are working with the right type of stream (e.g. BufferedStream
).
像这样的东西System.IO.File.ReadAllLines
可以代替System.IO.File.ReadAllText
大部分时间使用,并且ReadLine
可以在Read
您使用正确类型的流(例如BufferedStream
)后使用。
回答by Ekaterina
I had to remove the new lines all over the text. So I used:
我不得不删除整个文本的新行。所以我使用了:
while (text.Contains(Environment.NewLine))
{
text = text.Substring(0, text.Length - Environment.NewLine.Length);
}
回答by xr280xr
.Trim()
removes \r\n
for me (using .NET 4.0).
.Trim()
\r\n
为我删除(使用 .NET 4.0)。
回答by juhariis
As Markus pointed out TrimEnd is doing the job now. I needed to get line feeds and white space from both ends of string in Windows Phone 7.8 environment. After having chased different more complex options my problem was solved by using Trim() only - passed the following tests nicely
正如 Markus 指出的那样,TrimEnd 现在正在做这项工作。我需要在 Windows Phone 7.8 环境中从字符串的两端获取换行符和空格。在追逐不同的更复杂的选项后,我的问题仅通过使用 Trim() 解决了 - 很好地通过了以下测试
[TestMethod]
[Description("TrimNewLines tests")]
public void Test_TrimNewLines()
{
Test_TrimNewLines_runTest("\n\r testi \n\r", "testi");
Test_TrimNewLines_runTest("\r testi \r", "testi");
Test_TrimNewLines_runTest("\n testi \n", "testi");
Test_TrimNewLines_runTest("\r\r\r\r\n\r testi \r\r\r\r \n\r", "testi");
Test_TrimNewLines_runTest("\n\r \n\n\n\n testi ??l., \n\r", "testi ??l.,");
Test_TrimNewLines_runTest("\n\n\n\n testi ja testi \n\r\n\n\n\n", "testi ja testi");
Test_TrimNewLines_runTest("", "");
Test_TrimNewLines_runTest("\n\r\n\n\r\n", "");
Test_TrimNewLines_runTest("\n\r \n\n \n\n", "");
}
private static void Test_TrimNewLines_runTest(string _before, string _expected)
{
string _response = _before.Trim();
Assert.IsTrue(_expected == _response, "string '" + _before + "' was translated to '" + _response + "' - should have been '" + _expected + "'");
}