Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the second last indexof a value in a string?

Tags:

c#

asp.net

string referrer = "?404;http://stage.hello.com:80/Applications/";

referrer = referrer.Substring(referrer.LastIndexOf("/") + 1);

By doing this I get referrer value "" as answer.

I want my referrer value to be "Applications/"

How can I achieve this?

Any help will be greatly appreciated.

like image 392
Nishanth Suraj Avatar asked Dec 22 '15 10:12

Nishanth Suraj


2 Answers

referrer = referrer.Substring(referrer.Substring(0, referrer.LastIndexOf("/")).LastIndexOf("/") + 1);
like image 137
Steve Harris Avatar answered Sep 20 '22 11:09

Steve Harris


First variant:

referrer = Regex.Match(referrer, @"(?!/)[^/]*/[^/]*$").Value;

Second variant:

referrer = string.Join("/", referrer.Split('/').Reverse().Take(2).Reverse());
like image 43
Termininja Avatar answered Sep 18 '22 11:09

Termininja