Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortform way to replace two characters inside a string in C#?

Tags:

c#

Given:

var x = "abc";
var x = "[abc]";

I coded the following to remove the [ and ] characters.

x1 = x.Replace("[", "");
x2 = x1.Replace("]", "");

Is there some way that looks more clean to do this? Can I for example string one replace after the other? Note that the [ and ] if they appear will be found always at the start and the end of the string.

like image 586
Samantha J T Star Avatar asked Nov 21 '25 17:11

Samantha J T Star


2 Answers

string newstring = x.Trim('[', ']');
like image 138
Waqar Avatar answered Nov 23 '25 06:11

Waqar


Can I for example string one replace after the other?

Yes:

var result = x.Replace("[", "").Replace("]", "");
like image 43
Darin Dimitrov Avatar answered Nov 23 '25 06:11

Darin Dimitrov



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!