How to read the last line in a text box?

I have a multi-line text box that is constantly updated. I need to read only the last word / sentence in the text box.

string lastLine = textBox1.ReadLine.Last();
+5
source share
3 answers

Try the following:

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

And for the last word:

string lastword = lastLine.Split(' ').Last();
+9
source

The text field with the property MultiLineset to true has an array Linesfrom which it is easy to extract the required information

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

Here you need a little caution. If your text block still does not contain lines, you need to enter a check on the number of lines presented

+2
source

, :

string str = ....;

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

TextBox, :

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

Source: https://habr.com/ru/post/1610101/


All Articles