Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a list types and text source
var types= new List<string>()
                    {
                        "Volvo",
                        "Saab",                  
                    };
 
string source = "This car is Volvo and the other one is Saab";


and I want the words to be in bold as follows:

"This car is Volvo and the other one is Saab"

What I have tried:

I can do it in javascript using the RegExp using the following function:
function makeBold(source, types) {
    return source.replace(new RegExp('(\\b)(' + types.join('|') + ')(\\b)', 'ig'), '$1<bold>$2</bold>$3');
}

The question: How can I write the same function in C#? Or how to do it in C# in general?
Posted
Updated 17-Aug-21 10:30am
v3
Comments
PIEBALDconsult 17-Aug-21 16:05pm    
With a Regular Expression.
TempoClick 17-Aug-21 16:24pm    
can you show me how?
PIEBALDconsult 17-Aug-21 16:30pm    
Essentially the same as you show.

1 solution

Quote:
can you show me how?

Start here: Regex Class (System.Text.RegularExpressions) | Microsoft Docs[^]
And here: Regular Expression Language - Quick Reference | Microsoft Docs[^]
And download from here: Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900