Q:

Im trying to use indexof and substring to extract text from a file but the variable index is -1 all the time what is wrong? 

I have a html file with some strings inside for example:
"http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150000&cultuur=en-GB&continent=europa","http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150300&cultuur=en-GB&continent=europa","http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150600&cultuur=en-GB&continent=europa"

I ant to extract each line: http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150000&cultuur=en-GB&continent=europa
Then the next one: http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150300&cultuur=en-GB&continent=europa
This is the code im using:
In the constructor i did:
f = File. ReadAllText(localFilename + "test.html");
retrivingText1();

private void retrivingText1()
        {
            string startTag = "http://www.niederschlagsradar.de/images.aspx";//"<Translation>";
            string endTag = "continent=europa";//"</Translation>";
            int startTagWidth = startTag. Length;
            int endTagWidth = endTag. Length;
            index = 0;
            w = new StreamWriter(@"d:\retrivedText1.txt");
            while (true)
            {
                index = f.IndexOf(startTag, index);
                if (index == -1)
                {
                    break;
                }
                // else more to do - index now is positioned at first character of startTag 
                int start = index + startTagWidth;
                index = f.LastIndexOf(endTag, start + 1);
                if (index == -1)
                {
                    break;
                }
                // found the endTag 
                string g = f.Substring(start, index - start + endTagWidth).Trim(); //Trim the founded text so the start and ending spaces are removed. 
                w.WriteLine(g);
                //break so you dont have an endless loop
                break;
            }
            w.Close();
        }

I know for extracting from html file its better to use htmlagilitypack or regex.  But i wanted to try this time indexof and substring. 
When i use a breakpoint so on the line: 
int start = index + startTagWidth;

start = 2950
The next line after it index = -1

A:

I prefer Don's answer, but if you really want to use indexof it is much easier if you prime the loop and do something like this:
private void button3_Click(object sender, EventArgs e)
    {
        string f = "\"http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150000&cultuur=en-GB&continent=europa\",\"http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150300&cultuur=en-GB&continent=europa\",\"http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150600&cultuur=en-GB&continent=europa\"";

        int startIndex = 0;
        int endIndex = 0;
        int position = 0;

        string startTag = "http://www.niederschlagsradar.de/images.aspx";//"<Translation>";
        string endTag = "continent=europa";//"</Translation>";

        startIndex = f.IndexOf(startTag);

        while (startIndex > 0)
        {
            endIndex = f.IndexOf(endTag, position);

            //parse out what you want

            position = endIndex + endTag. Length;

            startIndex = (f.IndexOf(startTag, position));

            //something here to prevent endless loop
        }

    }

