I am searching for a string in a textbox in my web application. My output must be the line number of where the string can be found in the textbox.
To find the string in the textbox i am using IndexOf (which helps me make sure if the string is in the textbox or not). I know that if the string is in the textbox it will give me the number of characters as to where the string is.
Now is there a way of finding the line number???
(I was thinking that i go through the textbox starting from the first character all the way to string and as i pass by i check if there is a newline, but i dont know how to check if there is a new line in a textbox)

Split the text in the TextBox by '\n' search for each index in output array in the string and by refering to index you got the line number

i.e
You were my strength when I was weak
You were my voice when I couldn't speak
You were my eyes when I couldn't see

so my output array'd be like this
1- You were my strength when I was weak
2- You were my voice when I couldn't speak
3- You were my eyes when I couldn't see

foreach(string str in Text.Split("\n"))
{
//search for a string and return its index
}

the code you have shown is exactly what i need, but textbox doesnt hold the split() function :(
am i missing a reference or assembly
p.s. i am using a web applcations in asp.net

oh how stupid i am!!! i just realized.
will see if it works...

int getLine = 0;

        foreach (string str in TextBox1.Text.Split("n"))
        {
            int check = str.IndexOf(TextBox3.Text);
            int check1 = -1;

            if (check.Equals(check1))
            {
                getLine++;
            }
        }

        TextBox4.Text = Convert.ToString(getLine);

I am getting the following errors:

Error   1   The best overloaded method match for 'string.Split(params char[])' has some invalid arguments   
Error   2   Argument '1': cannot convert from 'string' to 'char[]'

Can you help please

Edited 3 Years Ago by Reverend Jim: Fixed formatting

i solved the errors, it had to come TextBox1.Text.Split('\n')
but now when i run the program and i input my strings to check if it is correct the program stops and gives me a "Server Error in '/' Application"
I am checking for the line number here http://localhost:3222/Default.aspx
maybe you can see what the error means, got a bit confused with it

This article has been dead for over six months. Start a new discussion instead.