簡體   English   中英

如何獲取URL路徑中的子目錄?

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

我正在尋找一種分割URL的方法,例如http:// aaa / bbb / ccc / ddd / eee

我怎么得到“ccc”? 當然可以拆分它,但它並不有趣。

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

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

我認為這是C#可以達到的最優雅的方式。

編輯:

實際上你也可以在這里使用myuri.Segments[2] ,給出相同的結果。 另請注意,此代碼返回"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