I have an unusual project in which I need to retrieve the text after the second-last occurrence of the character "\", effectively giving me the last two directories in the following example strings:
- D:\Archive Directory\2015-12-31 PM\SerialNo_01
- D:\Archive Directory\2016-01-01\SerialNo_02
- D:\Archive Directory\January 2016\SerialNo_03
The desired result is, respectively:
- 2015-12-31 PM\SerialNo_01
- 2016-01-01\SerialNo_02
- January 2016\SerialNo_03
I'd like to do this as cleanly as possible and preferably in one line of code for each string.
This question is being answered by me after finding nothing on Stack Overflow about finding the second-last occurrence (or, for that matter, any Nth occurrence going backwards) of a string or character within a string in c#. If the community finds this question is duplicated or feels it is too obscure a case, I am willing to remove it.
Edit: Clarified that I don't need to do this as a list of strings; they will be run one at a time. I'm dynamically adding them as radio button controls to a form.