I have two strings:
1 string = "stackoverflow"
2 string = "stackoverflow is good"
I want to show: "stackoverflow is good开发者_如何学Go
".
"is good" should be highlighted with some background-color..
how is it possible to do with c#?
First format all the text in the highlight-color then search for the "stackoverflow" and format it back to the normal format. This way you don't have to mess with the problem of finding something but formatting something else.
For technical details I have to know, what kind of control you use to display the text (Textbox, Rtf, Html).
static void Main(string[] args)
{
string strComplete = "stackoverflow is good, I mean, stackoverflow is really good";
string strSearch = "stackoverflow";
Console.WriteLine(FormatString(strComplete, strSearch));
Console.ReadKey();
}
private static string FormatString(string strComplete, string strSearch)
{
string strSpannedSearch = string.Format("{0}{1}{2}", "", strSearch, "");
return strComplete.Replace(strSearch, strSpannedSearch);
}
You can try out something on these lines
string s1 = "Hello";
string s2 = "Hello world";
s2= s2.Replace(s1, "");
Bitmap bmap = new Bitmap(150, 25);
Graphics graphic = Graphics.FromImage(bmap);
graphic.DrawString(s1, new Font(FontFamily.GenericSerif, 8), new SolidBrush(Color.White), new PointF());
graphic.DrawString(s2, new Font(FontFamily.GenericSerif, 8), new SolidBrush(Color.Yellow), new PointF());
bmap.Save("myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
精彩评论