In my code I will be presented with url strings to pages... All I want to do is to extract the page name, nothing else... (Example)
string page1 = "./default.aspx";
string page2 = "./subfolder/gallery.aspx";
string page3 = "./subfolder/anotherSubfolder/demo.aspx";
I've used the substring feature before, but I am struggling with this one since I only want the page names which are on the right hand side, I basically w开发者_运维技巧ant to chop of everything else (folder paths).. all I want to end up with is default.aspx, gallery.aspx and demo.aspx
Thanks in advance
Dal
Try using SubString with LastIndexOf:
string test = @"./subfolder/default.aspx";
test = test.Substring(test.LastIndexOf(@"/") + 1)
Either use a class for that (Uri might help?) or look at
yourString.Substring(yourString.LastIndexOf("/")+1)
You want to add error handling for the case where the string EndsWith a slash, though.
yourUrl.Split( new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries).Last();
Thanks
You can use FileInfo class.
See Nanme property.
FileInfo fi = new FileInfo( page3 );
DoSomething ( fi.Name );
精彩评论