1

在帖子上 C# 中从字符串中修剪换行符的最简单方法是什么?对我来说看起来相同的东西对他们有用,但它在我的字符串开头留下了新行。没有错误,它只是不修剪任何东西。

public formWords()
   {
   foreach (string word in someArray)
        {
            fileList += Environment.NewLine + word;
        }
        fileList.Trim(Environment.NewLine.ToCharArray());
        txtHolder.Text = fileList;
    }

我做错什么了?

提前致谢。

4

2 回答 2

2

使用此代码:

fileList.ToString().Trim( '\r', '\n' );

或使用代码(jon skeet

public static string TrimNewLines(string text)
{
    while (text.EndsWith(Environment.NewLine))
    {
        text = text.Substring(0, text.Length - Environment.NewLine.Length);
    }
    return text;
}
private static readonly char[] NewLineChars = Environment.NewLine.ToCharArray();

public static string TrimNewLines(string text)
{
    return text.TrimEnd(NewLineChars);
}
于 2013-07-10T03:46:30.953 回答
0
fileList +=word + Environment.NewLine;
于 2013-07-10T04:02:38.107 回答