I would like to remove %
characters from the start and the end of a string. For example:
string s = "%hello%world%";
Desired result: hello%world
.
I know that I can fix this with some if cases combined with StartsWith()
, EndsWith()
etc. But I'm looking for a cleaner solution.
I suppose regexp is the way to go here, and that's where I need your help.
There's no need for a regular expression. Just use this:
string result = input.Trim('%');
But if you really need a regular expression, you'd need to use start (^
) and end ($
) anchors, like this:
string result = Regex.Replace(input, "^%|%$", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With