Home > front end >  Find word in textbox and get line number with this word C#
Find word in textbox and get line number with this word C#

Time:08-14

I have a problem, I have a text in texbox1 and I would like to search for a word there, and if I find it, I would like to get the line number from texbox1 where the found word is... is there such a possibility because I have already dug the whole internet and I can not find a solution :(

I try this:

 private void button3_Click(object sender, EventArgs e)
{
    int iL = 0;
    
    foreach (string line in textBox1.Lines)
    {
        iL  = 1;
        if (line == "someword")
        {
            textBox2.Text = textBox1.Lines[iL].Remove(0, 15);
        }
    }

}

please help me because I am losing my mind :/

CodePudding user response:

I just do it by my self ;) so thx for help... And for others who want anwser..

 string line = textBox1.Lines.FirstOrDefault(l => l.Contains("test"));
            if (line != null)

            {
                textBox2.AppendText(line Environment.NewLine); //all line with string test
                textBox2.AppendText(line.Remove(0, 47)   Environment.NewLine);//only string test without 47 char from begining
            }

  • Related