how to replace?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to replace certain words in a string as below:

strTitle="Microsoft test title for project ABC";
strKeywords="title project";

what I want to do is: "Bold" the keywords "title" and "project" in the string strTitle.

Microsoft test <b>title</b> for <b>project</b> ABC
Any suggestions on how to do this one ? I am trying to do it in C#. Thanks!.
 
You can hard code it using string.Replace but if your strKeywords has
multiple words then it'd need to be parsed. Take a look at Brian's article
http://www.knowdotnet.com/articles/regereplacementstrings.html on
Regex.Replace - it should be pretty straightforward.

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
exBK said:
I need to replace certain words in a string as below:

strTitle="Microsoft test title for project ABC";
strKeywords="title project";

what I want to do is: "Bold" the keywords "title" and "project" in the string strTitle.

Microsoft test <b>title</b> for <b>project</b> ABC
Any suggestions on how to do this one ? I am trying to do it in C#.
Thanks!.
 
exBK,
In addition to the other comments.

Is the list of Keywords fixed or variable?

Is the Title fixed or variable length? (its it a "Title" or more of "Body
Text"?)

I would consider using StringBuilder.Replace coupled with String.Split.

Something like:

string strTitle = "Microsoft test title for project ABC";
string strKeywords = "title project";

System.Text.StringBuilder sb = new System.Text.StringBuilder(strTitle,
strTitle.Length * 2);

foreach(string word in strKeywords.Split(' '))
sb.Replace(word, "<B>" + word + "</B>");

strTitle = sb.ToString();

The "problem" with the above is "projects" will be converted to
"<B>project</B>s".

The alternative I would consider is a RegEx...

Hope this helps
Jay

exBK said:
I need to replace certain words in a string as below:

strTitle="Microsoft test title for project ABC";
strKeywords="title project";

what I want to do is: "Bold" the keywords "title" and "project" in the string strTitle.

Microsoft test <b>title</b> for <b>project</b> ABC
Any suggestions on how to do this one ? I am trying to do it in C#.
Thanks!.
 
Back
Top