Wrap a word in string with '<b>' to make it Bold without changing word's original case

.net, asp.net, c#

Solution

You can do this with a single `Regex.Replace` call as follows:

var result = Regex.Replace(
    "The allergy type a1c should be written A1C.", // input
    @"a1c",                                        // word to match
    @"<b>$0</b>",                                  // "wrap match in bold tags"
    RegexOptions.IgnoreCase);                      // ignore case when matching

Problem

I have a string and I need to make a specific word Bold by surrounding it with `<b>`, so that when it will be rendered the text must be bold. e.g. String word = "a1c" String myString = "The allergy type a1c should be written A1C." I can do the following: ``` String1.Replace(word,"<b>"+word+"<b>") ``` but it will change all the A1c word to "a1c" irrespective of the original word's case. ``` "The allergy type <b>a1c<b> should be written <b>A1C<b>." ``` How can I do it without changing case, so that i can get output as I know we can do it with a loop and index, but I wanted to know the best way that make use of advanced terms like RegEx or Linq or any small inbuilt machanism.

Original source

Related problems