简体   繁体   English

如何获取URL路径中的子目录?

[英]How to get a subdirectory in the path of a URL?

I am looking for a way to split a URL, such as http://aaa/bbb/ccc/ddd/eee . 我正在寻找一种分割URL的方法,例如http:// aaa / bbb / ccc / ddd / eee

How do I get "ccc"? 我怎么得到“ccc”? Of course it is possible to split it, but it is not interesting. 当然可以拆分它,但它并不有趣。

Uri myuri = new Uri("http://aaa/bbb/ccc/ddd/eee");

String str= myuri.Segments[myuri.Segments.Length-3];  

I think this is the most elegant way you can reach by C#. 我认为这是C#可以达到的最优雅的方式。

EDIT: 编辑:

Actually you can also go with myuri.Segments[2] here, there give same result. 实际上你也可以在这里使用myuri.Segments[2] ,给出相同的结果。 Also note that this code returns "ccc/" as result, so if you want to get "ccc" you can go by this(also elegant) way. 另请注意,此代码返回"ccc/"作为结果,因此如果您想获得"ccc"您可以通过此(也是优雅的)方式。

String str= myuri.Segments[myuri.Segments.Length-3].TrimEnd('/');  

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

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