在C#中获取URL的特定部分

use*_*795 -1 c# string-formatting

我在字符串变量中有一个Url.

我所拥有的Urls的示例格式是:

http://something.com/sites/collection
Run Code Online (Sandbox Code Playgroud)

http://something:33/sites/collection, something:44/sites/collection

现在我想从该URL中提取"/ sites/collection"部分.

Lee*_*Lee 6

您可以创建System.Uri实例并使用该AbsolutePath属性:

var uri = new Uri("http://something.com/sites/collection");
string path = uri.AbsolutePath;
Run Code Online (Sandbox Code Playgroud)