2009-06-24 99 views
82

我想确保_content不以新行字符结尾:C#中修剪字符串的换行符最简单的方法是什么?

_content = sb.ToString().Trim(new char[] { Environment.NewLine }); 

,但上面的代码不起作用因为修剪,似乎没有为字符串的集合重载的参数,只字符。

什么是从字符串的末尾去除Enivronment.Newline的最简单的单行程?

+3

顺便说一句,你可能想TrimEnd – 2009-06-24 12:28:51

回答

181

以下适用于我。

sb.ToString().TrimEnd('\r', '\n'); 

sb.ToString().TrimEnd(Environment.NewLine.ToCharArray()); 
+0

非常简洁,涉及的是\ r \ n问题,很好 – 2009-06-24 13:28:46

+1

虽然修剪字符组成一个新行字符(S)的任意组合确实解决根本问题和问题的精神,它也在一般意义上留下了不必要的功能。在文件处理过程中,我可以看到需要将第一个\ n完整保留在HeresALineForUnixToBeFedFromWindows的一个字符串中\ n \ r \ n。尽管更详细的,约翰飞碟双向的答案很可能是最好的和斯科特温斯坦公司的ReadLine也更“准确”(尽管也许有一点开销) – 2013-01-14 23:07:02

+16

TrimEnd()已经在做的工作不带任何参数! http://msdn.microsoft.com/de-de/library/system.string.trimend(v=vs.110).aspx和http://msdn.microsoft.com/de-de/library/system.char .iswhitespace(v = vs.110).aspx – Markus 2014-03-31 09:42:17

5

什么

_content = sb.ToString().Trim(Environment.NewLine.ToCharArray()); 
+0

这并不做* *相当同样的事情 - 它”例如,即使Environment.NewLine是crlf – 2009-06-24 12:28:20

+0

将“abc \ n \ n \ n \ n \ n”声明为特征:-)我喜欢解决方案。 – 2009-06-24 12:37:12

13

如何:

public static string TrimNewLines(string text) 
{ 
    while (text.EndsWith(Environment.NewLine)) 
    { 
     text = text.Substring(0, text.Length - Environment.NewLine.Length); 
    } 
    return text; 
} 

它如果有多个新行有点低效,但它会工作。

另外,如果你不介意它修剪(说)"\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); 
} 
+0

可能是最准确的解决方案,而不是一个班轮:-) – bang 2009-06-24 12:35:18

4
_content = sb.TrimEnd(Environment.NewLine.ToCharArray()); 

当然这将删除 “\ r \ r \ r \ r”以及“\ n \ n \ n \ n”等组合。 而在“enviroments”里的换行不是“\ n \ r”你可能会得到一些奇怪的行为:-)

但是如果你可以用这个活那么我相信这是为了去除新线的最有效实现了其他的方式字符串末尾的字符。

8

使用框架。所述的ReadLine()方法具有以下的说:

的线被定义为后跟行馈送 (“\ n”个)的 字符的序列,一个回车(“\ R”)或一个 回车后立即跟着 换行(“\ r \ n”)。返回的字符串 不包含 终止回车或行 供稿。

所以下面会做的伎俩

_content = new StringReader(sb.ToString()).ReadLine(); 
2

如何只:

string text = sb.ToString().TrimEnd(null) 

这将拉动所有空格字符从字符串的结尾 - 如果你只有一个问题想要保留非换行符空白。

0

有点没有答案,但从字符串中删除换行符的最简单方法是首先不要在字符串上放置换行符,因为确保它不会被自己的代码看到。也就是说,通过使用删除换行符的原生函数。如果您逐行询问输出,许多流和文件/ io方法将不包含换行符,尽管可能需要在System.IO.BufferedStream中包含某些内容。

之类的东西System.IO.File.ReadAllLines可以代替System.IO.File.ReadAllText大部分时间使用,并且可以ReadLine一旦你用正确类型的流(例如BufferedStream)工作可以用来代替Read

-1

我不得不从头再来的文字中的新线路。所以我用:

  while (text.Contains(Environment.NewLine)) 
      { 
       text = text.Substring(0, text.Length - Environment.NewLine.Length); 
      } 
15

.Trim()删除\r\n对我来说(使用.NET 4.0)。

0

正如马库斯指出,现在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 + "'"); 
    } 
相关问题