Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何查找字符串中值的最后一个索引?_C#_Asp.net - Fatal编程技术网

C# 如何查找字符串中值的最后一个索引?

C# 如何查找字符串中值的最后一个索引?,c#,asp.net,C#,Asp.net,通过这样做,我得到了referer值“作为答案 我希望我的推荐人值为“Applications/” 我怎样才能做到这一点 任何帮助都将不胜感激。第一种变体: string referrer = "?404;http://stage.hello.com:80/Applications/"; referrer = referrer.Substring(referrer.LastIndexOf("/") + 1); referrer = referrer.Substring(referrer.Su

通过这样做,我得到了referer值
作为答案

我希望我的推荐人值为“Applications/”

我怎样才能做到这一点

任何帮助都将不胜感激。

第一种变体:

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

referrer = referrer.Substring(referrer.LastIndexOf("/") + 1);
referrer = referrer.Substring(referrer.Substring(0, referrer.LastIndexOf("/")).LastIndexOf("/") + 1);
第二种变体:

referrer = Regex.Match(referrer, @"(?!/)[^/]*/[^/]*$").Value;
不安全变量(如果您确定字符串中的值至少出现两次)如下所示

referrer = string.Join("/", referrer.Split('/').Reverse().Take(2).Reverse());
string referrer = "?404;http://stage.hello.com:80/Applications/";
referrer = referrer.Substring(referrer.LastIndexOf("/", referrer.LastIndexOf("/") - 1) + 1);
安全版本是这样的

referrer = string.Join("/", referrer.Split('/').Reverse().Take(2).Reverse());
string referrer = "?404;http://stage.hello.com:80/Applications/";
referrer = referrer.Substring(referrer.LastIndexOf("/", referrer.LastIndexOf("/") - 1) + 1);