Get link from HTML code anchor tag

Task: Get Link from HTML code anchor <a> tag using Asp.net and C# code.

Description:  Sometime we need to retrieve link from HTML code <a> tag Or sometime we need to modify/append/alter that link.
So in this function the HTML code is use as input and then we can retrieve link in that html code <a> tag.

Code: 

public void getlinkfromhtml(string content)
{
  int index = 0;
  do
  {
    int newindex = index + 5;
    index = content.IndexOf("href=", newindex);
    if (index > 0)
    {
      int index2 = content.IndexOf('\'', index + 6);
      int index3 = content.IndexOf('\"', index + 6);

      if (index3 > 0)
      {
        if (index2 > index3 || index2 == (-1))
            index2 = index3;
      }
      if (index2 > 0)
      {
        string href = content.Substring(index+6,(index2-(index + 6))).ToLower();

// Save/Use or do anything with this link href. You Can replace also this with another link.

      }
    }
  }
  while (index > 1);
}

1 comment: