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



How to Highlight a specific word in HTML content (C#)
This code snippet shows how to hightlight (or apply a certain style to) a specific word (or combination of words) from a striong in a browser.
public static string HighLight(string in_Text, string in_TextToHighLight, string lv_Style)
{
  int lv_Pointer = 0;

  while (lv_Pointer > -1)
  {
    lv_Pointer = in_Text.ToLower().IndexOf(in_TextToHighLight.ToLower(),lv_Pointer);
    if (lv_Pointer >= 0)
    {
      in_Text = in_Text.Substring(0,lv_Pointer) + 
            "" +
            in_Text.Substring(lv_Pointer,in_TextToHighLight.Length) +
            "" + 
            in_Text.Substring(lv_Pointer + in_TextToHighLight.Length);
     
      lv_Pointer = lv_Pointer + 14 + lv_Style.Length + in_TextToHighLight.Length;
    }
  }

  return in_Text;
}
The function returns a HTML formatted string containing the original in_Text with a style (parameter: lv_Style) applied to the given words (parameter: in_TextToHighLight). To change all words 'Internet' to the font color 'red' call the funtion like this:
string MyResultHTML = HighLight(MyOriginalHTML, "internet", "font-color:red");

Labels: , ,


Posted by Xander Zelders
0 Comments



How to remove HTML-tags from web content (C#) (16 April 2007)
Using webcontent in applications can be very annoying since webcontent usually contains lots of HTML elements. With one simple action, using regular expressions, all of these HTML elements can be removed from the content. What's left is a clean string, without HTML formatting. Snippet:
  using System.Text.RegularExpressions;

  ...

  public static string RemoveHTML(string in_HTML)
    {
      return Regex.Replace(lv_HTML, "<(.|\n)*?>", "");
    }

Labels: ,


Posted by Xander Zelders
5 Comments



 
Previous Posts
    - Remove content between HEAD tags using Regex and c...
    - Strip HTML tags, HEAD content and SCRIPT tags from...
    - 9 Tips for creating indexes in SQL Server
    - Performance Tip 1: Avoid non-sargable WHERE-clause...
    - 23 Tips to improve the performance of your SQL que...
    - A cheat sheet for SQL Server developers
    - How to replace certain word with a hyperlink using...
    - 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

Archives
    - April 2007
    - May 2007
    - April 2008
    - December 2008


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