Home | Index | Dotnet4all Snippets | Submit resources
About | Mail us 
How to replace certain word with a hyperlink using C# (09 May 2007)


 
This code shows, using C#, how to build a function that replaces certain word (-combinations)with a hyperlink and returns a HTML formatted string.
public static string InsertHyperLink(string in_Text, string in_TextToHyperLink, string in_HyperLink)
{
  int lv_Pointer = 0;
  while (lv_Pointer > -1 && lv_Pointer < in_Text.Length)
  {
    lv_Pointer = in_Text.ToLower().IndexOf(in_TextToHyperLink.ToLower(),lv_Pointer);
    if (lv_Pointer >= 0)
    {

    in_Text = in_Text.Substring(0,lv_Pointer) + 
        "<a href=\"" + in_HyperLink + "\" style=text-decoration:none;font-size:11px;>" +
        in_Text.Substring(lv_Pointer,in_TextToHyperLink.Length) +
        "</a>" + 
        in_Text.Substring(lv_Pointer + in_TextToHyperLink.Length);
     
    lv_Pointer = lv_Pointer + 78 + in_HyperLink.Length + in_TextToHyperLink.Length;
    }
  }
  return in_Text;
}

Labels: , ,


Posted by Xander Zelders



0 Comments:

Post a Comment

<< Home

 
Previous Posts
    - How to Highlight a specific word in HTML content (...
    - how to extract SRC from IMG elements in HTML code
    - How to extract URL and Anchor from HTML content
    - Grab the content of a (GZIP) webpage using C#
    - How to extract the host name from an URL (C#)
    - How to Send an email using SMTP (C#)
    - How to remove HTML-tags from web content (C#)
    - How to convert DateTime to SQL valid string



Disclaimer & Terms of Use | DotNet4All.Com concept & © 2004 - 2007 by Zelders² - Holland