从字符串生成器/字符串 c#

问题描述 投票:0回答:7

好吧,我正在尝试在表单中制作一个像文本框一样的“控制台”,但是一旦到达底部,无法向上滚动,它只会删除顶行,我遇到了一些困难。 到目前为止,当它到达底部时,它会删除顶行,但只有一次,它会照常进行。这是我的功能:

   StringBuilder sr = new StringBuilder();
    public void writeLine(string input)
    {
        string firstline = "";
        int numLines = Convert.ToString(sr).Split('\n').Length;
        if (numLines > 15)      //Max Lines
        {                
            sr.Remove(0, Convert.ToString(sr).Split('\n').FirstOrDefault().Length);              
        }
        sr.Append(input + "\r\n");
        consoleTxtBox.Text = Convert.ToString(sr) + numLines;
    }

如果有人能解决这个问题就太好了,谢谢

卢卡斯

c# stringbuilder
7个回答
11
投票

首先,您的解决方案有什么问题:它不起作用的原因是它删除了该行的内容,但它忽略了末尾的

\n
。添加
1
应该解决这个问题:

sr.Remove(0, Convert.ToString(sr).Split('\n').FirstOrDefault().Length+1);              
//                                                                    ^
//                                                                    |
//   This will take care of the trailing '\n' after the first line ---+

现在用更简单的方法来做:你需要做的就是找到第一个

\n
,然后在它后面取子串,就像这样:

string RemoveFirstLine(string s) {
    return s.Substring(s.IndexOf(Environment.NewLine)+1);
}

请注意,即使字符串中没有换行符,此代码也不会崩溃,即当

IndexOf
返回
-1
(在这种情况下,没有任何内容被删除)。


3
投票

您可以使用 TextBox 中的 Lines 属性。这将获取 TextBox 中的所有行,作为一个数组,然后创建一个不包含第一个元素 (

Skip(1)
) 的新数组。它将这个新数组分配回文本框。

string[] lines = textBox.Lines;
textBox.Lines = lines.Skip(1).ToArray();

2
投票

一个简单的替代方案:您可以通过

Environment.NewLine
拆分字符串并返回除第一个以外的所有内容:

public static string RemoveFirstLine(string input)
{
    var lines = input.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
    return string.Join(Environment.NewLine, lines.Skip(1));
}

演示


2
投票

你可以删除这条线

 var lines = lines.Remove(0, lines.ToString().IndexOf(Environment.NewLine));

1
投票

大多数解决方案似乎没有考虑到 Enviroment.NewLine 可以包含多个字符(len > 1)这一事实。

    public void RemoveFirstStringFromStringBuilder()
    {
        var lines = new StringBuilder();

        lines.AppendLine("abc");

        var firstLine = lines.ToString().IndexOf(Environment.NewLine, StringComparison.Ordinal);

        if (firstLine >= 0)
            lines.Remove(0, firstLine + Environment.NewLine.Length);

        Console.WriteLine(lines.Length);
        Console.WriteLine(lines.ToString());
    }

打印出:0 和 ""


0
投票

对我有用的是:

    var strBuilder = new StringBuilder();
    strBuilder.AppendLine("ABC");
    strBuilder.AppendLine("54");
    strBuilder.AppendLine("04");

    strBuilder.Remove(0, strBuilder.ToString().IndexOf(Environment.NewLine) + 2);

    Console.WriteLine(strBuilder);

+1 的解决方案对我不起作用,可能是因为 EOF 在这种情况下被解释为 2 个字符( )


0
投票

只删除第一行是一个糟糕的解决方案。如果您一次附加多行并用换行符分隔怎么办?以下是一种更好的方法,其中代码确保最多有一定数量的行:

public int maxLines = 50;

public void RemoveSuperfluousLines(ref string text)
{
    // A line is defined by the NewLine sequence. Match all NewLines in the text.
    MatchCollection matches = Regex.Matches(text, Environment.NewLine);
    // Find the index of the match directly preceding the first line we want to allow.
    int index = matches.Count - maxLines;
    // A negative index means we have less than maxLines number of lines.
    if (index >= 0)
        // Remove everything up to and including the match.
        text = text.Remove(0, matches[index].Index + matches[index].Length);
}

public void AppendLine(ref string text, string line)
{
    // Append the line preceded by a NewLine to the text.
    text = text + Environment.NewLine + line;
    // Remove superfluous lines.
    RemoveSuperfluousLines(ref text);
}

用法:

maxLines = 15;

// Write more than 15 lines, lol!
for (int index = 0; index < 50; index++)
{
    AppendLine(ref text, index.ToString());
}

consoleTxtBox.Text = text;
© www.soinside.com 2019 - 2024. All rights reserved.