Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing string characters in c#

Tags:

string

c#

asp.net

I have a string which is a sentence or two long (more than one word). In that sentence there will be a hash-tagged word e.g. #word. This needs to be replaced with *word*.

If the sentence is:

Today the weather is very nice #sun

It should become:

Today the weather is very nice *sun*

How would I go about doing this?

like image 955
Rothmanberger Avatar asked Jun 02 '26 08:06

Rothmanberger


1 Answers

You could do a regular expression, like this:

var output = Regex.Replace(input, @"#(\w+)", "*$1*");
like image 67
p.s.w.g Avatar answered Jun 05 '26 01:06

p.s.w.g