Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient way to change the last element in a '/' delimited string

Tags:

c#

I have a string object in c# with a bunch of elements delimited by '/' characters. The string will look something like this:

"element1/element2/element3/element4"

What's the most efficient way to change the last element in the '/' delimited string?

like image 974
LandonSchropp Avatar asked Jan 19 '26 14:01

LandonSchropp


1 Answers

Use string.LastIndexOf:

string s = "element1/element2/element3/element4";
s = s.Substring(0, s.LastIndexOf('/') + 1) + "foo";
like image 190
Mark Byers Avatar answered Jan 22 '26 04:01

Mark Byers



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!