How to read the last line in a textbox?

后端 未结 3 854
花落未央
花落未央 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:12

    You can extract the last line from any string as follows:

    string str = ....;
    
    string[] lines = str.Split('\n', '\r');
    string last_line = lines[lines.Length - 1];
    

    To get the last line of a TextBox you can use:

    string[] lines = textBox1.Text.Split('\n', '\r');
    string last_line = lines[lines.Length - 1];
    

提交回复
热议问题