2012-11-09 210 views

回答

6

自认为是一个路径/ URL,使用Path.GetFileNameWithoutExtension

string name = Path.GetFileNameWithoutExtension(path); 

我宁愿Path,但如果你想使用Uri类代替,你也可以使用这个:

Uri uri = new Uri("https://company.zendesk.com/api/v2/tickets/33126.json"); 
string lastSegment = uri.Segments.Last(); 
string name = lastSegment.Substring(0, lastSegment.IndexOf('.')); 
+0

那么这是一个URL而不是路径,因此......'Path.GetFileNameWithoutException * *可能会工作,但我想我会正在寻找URL类... –

+0

是的,这是一个URL,将路径工作没有任何问题?! –

+0

这工作。 thx :) –

0

这看起来像正则表达式的工作。

 Regex regex = new Regex(@"https://company.zendesk.com/api/v2/tickets/(\d+).json"); 

     Match match = regex.Match("https://company.zendesk.com/api/v2/tickets/33126.json"); 

     foreach(Group group in match.Groups) 
     { 
      Console.WriteLine(group.Value); 
     } 
相关问题