r.r
r.r

Reputation: 7153

need to show changed text after comparing two strings

I have two strings:

1 string = "stackoverflow"

2 string = "stackoverflow is good"

I want to show: "stackoverflow is good".

"is good" should be highlighted with some background-color..

how is it possible to do with c#?

Upvotes: 4

Views: 1128

Answers (2)

Alexander Schmidt
Alexander Schmidt

Reputation: 5723

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);            
}

Upvotes: 5

V4Vendetta
V4Vendetta

Reputation: 38210

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);

Upvotes: 1

Related Questions