Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get substring after a pattern in C#

Tags:

c#

Looking to get the substring after the last comma or the last pipe. So say I have a string that could be either: "pipe|delimited|string" or "comma,delimited,string", the result should be "string" in both cases.

I'm looking at C#'s substring on a lastindexof, but I don't know how to do it for both pipes and commas. Something like this: str.Substring(str.LastIndexOf('|' or ',')). Any ideas?

like image 609
Los Morales Avatar asked Feb 23 '26 02:02

Los Morales


1 Answers

There's a nice function for that.

str.LastIndexOfAny(new []{',','|'});
like image 111
Keith Nicholas Avatar answered Feb 25 '26 16:02

Keith Nicholas



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!