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: formatting, HighLight, HTML
Posted by Xander Zelders