Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific characters from string C#

I have got a string ["foo","bar","buzz"] from view, i want to remove [,"&,]
I have used string x = tags.Trim(new Char[] { '[', '"', ']' }); but the output i got is foo","bar","buzz instead of foo,bar,buzz

enter image description here

I have tried Trimming & this but still having problem.

like image 309
Fawad Bin Tariq Avatar asked Mar 14 '26 06:03

Fawad Bin Tariq


1 Answers

As alternative, you can use a "simple" Replace

string x = tags.Replace("[","")
               .Replace("\"","")
               .Replace("]","");

It isn't fast, but it's simple.

If you need more performance you should use an alternative.


please note: that each Replace call returns a new string, and with every call the whole string is reevaluated. Although I often use this myself (due to the readability) it's not recommended for complex patterns or very long string values
like image 184
Stefan Avatar answered Mar 15 '26 21:03

Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!