简体   繁体   English

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

[英]Get page name from URI, substring between two characters

I want to get the page name from a URI for instance if I have 我希望从URI获取页面名称,例如我有

"/Pages/Alarm/AlarmClockPage.xaml"

I want to get AlarmClockPage 我想获得AlarmClockPage

I tried 我试过了

//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;
 }

But because the string being search may contain one or more forward slashes, I don't think this method work in such case. 但是因为正在搜索的字符串可能包含一个或多个正斜杠,所以我认为这种方法在这种情况下不起作用。

So how do I consider the multiple forward slashes that may present? 那么我该如何考虑可能出现的多个正斜杠呢?

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

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

If you only want to use string functions, you may try: 如果您只想使用字符串函数,可以尝试:

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

It gives file name from a given file path. 它从给定的文件路径中提供文件名。 try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM