简体   繁体   中英

remove first line from string builder / string c#

Okay so I'm trying to make a 'console' like text box within a form, however once you reach the bottom, instaid of being able to scroll up, it will just delete the top line, Im having some difficulties. So far, when it gets to bottom it deletes the top line, however only once, it just carries on as normal. Here is my function:

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

Would be great if someone could fix this, thanks

Lucas

First, what's wrong with your solution: the reason it does not work is that it removes the content of the line, but it ignores the \\n at the end. Adding 1 should fix that:

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

Now to doing it a simpler way: all you need to do is finding the first \\n , and taking substring after it, like this:

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

Note that this code does not crash even when there are no newline characters in the string, ie when IndexOf returns -1 (in which case nothing is removed).

You can use the Lines property from the TextBox. This will get all the lines in the TextBox, as an array, then create a new array that doesn't include the first element ( Skip(1) ). It assigns this new array back to the textbox.

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

A simple alternative: you could split the string by Environment.NewLine and return all but the first:

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

Demo

你可以删除这一行

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

Most solutions does not seem to take into account the fact that Enviroment.NewLine can consist of multiple characters (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());
    }

Prints out: 0 and ""

What worked for me is:

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

Solution with +1 didn't work for me, probably because of EOF in this context being interpreted as 2 chars (\\r\\n)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM