Weird Substring Error

R

rh1200la

Hey all. I'm doing a very basic screen scrape of a page that displays
a banner ad. It is basically an <A> tag and an <IMG> tag. I need to
grab the url of each and store them.

Right now I can download the page fine into my code but I'm having a
problem on this line:

imgTag = m_strSite.Substring(imageStart, imageEnd);

I get the following error:

Index and length must refer to a location within the string. Parameter
name: length

the strange thing is when I use Substring and use a start value greater
than 5 I get the error.


Here is my code:

private string m_strSite;

private void Page_Load(object sender, System.EventArgs e)
{

string currURL = "http://someurl.com?vars";
m_strSite = OpenSite(currURL);


int imageStart;
int linkStart = 0;
int linkEnd;
int imageEnd = m_strSite.Length - 6 ;
string urlTag;
string imgTag;

imageStart = Find("<img", 0);
linkEnd = imageStart;


urlTag = m_strSite.Substring(linkStart, linkEnd);
imgTag = m_strSite.Substring(imageStart, imageEnd);

}

private int Find(string strSearch, int nStart)
{
return m_strSite.IndexOf(strSearch, nStart);
}
 
J

Jon Skeet [C# MVP]

Hey all. I'm doing a very basic screen scrape of a page that displays
a banner ad. It is basically an <A> tag and an <IMG> tag. I need to
grab the url of each and store them.

Right now I can download the page fine into my code but I'm having a
problem on this line:

imgTag = m_strSite.Substring(imageStart, imageEnd);

I get the following error:

Index and length must refer to a location within the string. Parameter
name: length

the strange thing is when I use Substring and use a start value greater
than 5 I get the error.

Substring doesn't take a start index and an end index - it takes a
start index and a *length*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top