从string builder / string c#中删除第一行

本文关键字:string 一行 删除 builder | 更新日期: 2023-09-27 18:10:42

好的,所以我试图在一个表单中创建一个'控制台'式的文本框,但是一旦你到达底部,而不是能够向上滚动,它只会删除顶行,我有一些困难。到目前为止,当它到达底部时,它删除了顶部的一行,但只有一次,它只是照常进行。下面是我的函数:

   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;
    }

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

卢卡斯

从string builder / string c#中删除第一行

首先,您的解决方案有什么问题:它不起作用的原因是它删除了该行的内容,但忽略了末尾的'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时(在这种情况下没有删除任何内容)。

你可以使用文本框的Lines属性。这将获得TextBox中的所有行,作为一个数组,然后创建一个不包括第一个元素(Skip(1))的新数组。它将这个新数组赋给文本框。

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

一个简单的选择:您可以按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));
}

你可以删除这一行

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

大多数解决方案似乎没有考虑到环境。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和"

我的做法是:

    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个字符('r'n)

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

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;

这个版本是有效的,并处理所有可能的变化的新行字符("'r'n", "' & ", "'n")。

char[] NewLineChars = { ''r', ''n' };
string RemoveFirstLine(string s)
{
    int i = s.IndexOfAny(NewLineChars);
    if (i < 0)
        return s;
    char c = s[i++];
    if (c == NewLineChars[0] && i < s.Length && s[i] == NewLineChars[1])
        i++;
    return s.Substring(i);
}