For ASP.NET,
wrap the words you want highlighted in a <span>
. Then set the <span>
style background-color
to the colour of your choice, or use a CSS class to do so.
For example,
<asp:Label runat="server">
<span style="background-color:Blue;">Hello</span> World
</asp:Label>
or
<asp:Label runat="server" Text="<span style='background-color:Blue;'>Hello</span> World" />
EDIT:
If setting this in code behind, then you can do something like the following
StringBuilder builder = new StringBuilder();
builder.Append([start of text]);
builder.Append("<span style=\"background-color:Blue;\">");
builder.Append([text to highlight]);
builder.Append("</span>");
builder.Append([rest of text]);
Label.Text = builder.ToString();
If you needed to match text already in the label against some specific text then something like the following
string theTextToMatch = "[Text to match]";
string theText = Label.Text;
int leftIndex = theText.IndexOf(theTextToMatch, StringComparison.OrdinalIgnoreCase);
int rightIndex = leftIndex + theTextToMatch.Trim().Length;
StringBuilder builder = new StringBuilder();
builder.Append(theText , 0, leftIndex);
builder.Append("<span style=\"background-color:Blue;\">");
builder.Append(theText, leftIndex, rightIndex - leftIndex);
builder.Append("</span>");
builder.Append(theText, rightIndex, theText.Length - rightIndex);
Label.Text = builder.ToString();