C# 从URI获取页面名称,两个字符之间的子字符串

C# 从URI获取页面名称,两个字符之间的子字符串,c#,windows-phone-8,substring,C#,Windows Phone 8,Substring,例如,如果我有 "/Pages/Alarm/AlarmClockPage.xaml" 我想获取AlarmClockPage 我试过了 //usage GetSubstring("/", ".", "/Pages/Alarm/AlarmClockPage.xaml") public static string GetSubstring(string a, string b, string c) { string str = c.Substring((c.IndexOf(a) +

例如,如果我有

"/Pages/Alarm/AlarmClockPage.xaml"
我想获取
AlarmClockPage

我试过了

//usage GetSubstring("/", ".", "/Pages/Alarm/AlarmClockPage.xaml")
 public static string GetSubstring(string a, string b, string c)
 {  
     string str = c.Substring((c.IndexOf(a) + a.Length),
          (c.IndexOf(b) - c.IndexOf(a) - a.Length));

     return str;
 }
但是由于正在搜索的字符串可能包含一个或多个斜杠,我认为这种方法在这种情况下不起作用


< P> >我如何考虑可能出现的多个前斜杠?

为什么不使用已经在框架中的方法?

System.IO.Path.GetFileNameWithoutExtension(@"/Pages/Alarm/AlarmClockPage.xaml");

如果您只想使用字符串函数,可以尝试:

var startIdx = pathString.LastIndexOf(@"/");
var endIdx = pathString.LastIndexOf(".");
if(endIdx!=-1)
{
    fileName = pathString.Substring(startIdx,endIdx);
}
else
{
    fileName = pathString.Substring(startIdx);
}

它给出给定文件路径中的文件名。试试这个

string pageName = System.IO.Path.GetFileName(@"/Pages/Alarm/AlarmClockPage.xaml");

谢谢我是否必须在路径参数中包含
@
?@PutraKg-仅当字符串中有要转义的字符时。在提供的示例中,这不是必需的。为什么不使用内置函数呢?它可以处理正斜杠或反斜杠。当然我会使用它们,我只是建议这样做,因为op正试图用@wiredparie这样做