如何在字符串的特定子字符串之后获取字符串?

问题描述:

可能重复:
How do I extract PART of a specific string from a huge chunk of ugly strings?如何在字符串的特定子字符串之后获取字符串?

字符串:

https://company.zendesk.com/api/v2/tickets/33126.json 

我需要数33126.

什么来提取这一数字的最佳方式?

+2

de-ja-vu?我认为我在 –

+0

之前看到过这个问题,你问的是同样的问题.... – Anirudha

自认为是一个路径/ 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 :) –

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

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