3

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.

Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
  • 1
    http://stackoverflow.com/questions/1139439/how-do-you-do-case-insensitive-string-replacement-using-regular-expressions – andy Nov 28 '13 at 12:57
  • http://stackoverflow.com/questions/3993826/case-insensitive-replace-without-using-regular-expression-in-c – andy Nov 28 '13 at 12:58
  • 2
    @Rawling Please see the question again, I've made changes to explain it better. – Imran Rizvi Nov 28 '13 at 13:07
  • Take a look at the links provided by andy. Also try [google](https://www.google.be/#q=c%23+string+replace+case+insensitive) which will provide a lot of answers for you. You just have to pick one that suits you best. – Koen Nov 28 '13 at 13:17
  • 1
    @andy that will help him MATCH both words... but will it actually help him REPLACE it, keeping each case? Or will it replace both versions with a lowercase version? – MikeSmithDev Nov 28 '13 at 13:18
  • @andy I think both the link say about finding word case-insensitive and replacing with the one we put , they does not look for case of a word in string. – Imran Rizvi Nov 28 '13 at 13:23
  • @MikeSmithDev MY apologies, I didn't mean to make you delete your answer, especially since you were apparently first to figure out what OP was actually asking :) – Rawling Nov 28 '13 at 13:56
  • @Rawling No prob. I thought your answer had more value, but I guess the loop in my answer could be helpful for certain situations. – MikeSmithDev Nov 28 '13 at 14:38

3 Answers3

9

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
Rawling
  • 49,248
  • 7
  • 89
  • 127
5

You can use a Regex to match the word, regardless of case:

string word = "a1c";
string myString = "The allergy type a1c should be written A1C.";
var regex = new Regex(word, RegexOptions.IgnoreCase);
foreach (Match m in regex.Matches(myString))
{
    myString = myString.Replace(m.ToString(), "<b>" + m.ToString() + "</b>");
}

PS. Consider a different route for bolding! Inline styling is not ideal.

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
0

Check this code this is exactly I think you are looking for

string strRegex = @"<b>(?<X>.*?)</b>";
RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = "Hellow world is my <b>first</b> application in <b>computer</b> world.";
        string NewString = strTargetString;
        foreach (Match myMatch in myRegex.Matches(strTargetString))
        {
            if (myMatch.Success)
            {
                NewString = NewString.Replace(myMatch.ToString(), myMatch.ToString().ToUpper());
            }
        }

Output in NewString

Hellow world is my FIRST application in COMPUTER world

Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58