How to read the last line in a textbox?

后端 未结 3 815
花落未央
花落未央 2021-01-13 03:57

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(         


        
3条回答
  •  借酒劲吻你
    2021-01-13 04:27

    A textbox with the MultiLine property set to true has an array of Lines from which it is easy to extract the info required

    int maxLines = textBox1.Lines.Length;
    if(maxLines > 0)
    {
        string lastLine = textBox1.Lines[maxLines-1];
        string lastWord = lastLine.Split(' ').Last();
    }
    

    A bit of caution here is needed. If your textBox still doesn't contain any lines you need to introduce a check on the number of lines present

提交回复
热议问题