Search code examples
c#stringlinqtextboxreadlines

How to read the last line in a textbox?


I have a multiline textbox that constantly gets updated. I need to read only the last word/sentence in the textbox.

string lastLine = textBox1.ReadLine.Last();

Solution

  • Try this:

    if (textBox1.Lines.Any())
    {
        string lastLine = textBox1.Lines[textBox1.Lines.Length - 1];
    }
    

    And for last word:

    string lastword = lastLine.Split(' ').Last();