将URL修剪到最后一个路径,直到到达主机

时间:2013-07-14 02:07:19

标签: .net vb.net

我编写了少量代码来修剪一个url到最后一个路径,直到达到主机,但它不能正常工作:

Dim line As String
line = "http://www.domain.com/folder1/folder2/folder3/folder4/"
line = Replace(line, "http://", "")

Dim count As Integer
count = line.Split("/").Length - 1

For x = count To 1 Step -1
    count = line.Split("/").Length - 1
    Dim lastpath As String = line.Split("/")(x).Split("/")(0)
    Dim newline As String = Replace(line, "/" & lastpath, "")
    MsgBox(newline)

在我的场景中,我希望看到带有网址的5条消息,而不是最后一条路径:

www.domain.com/folder1/folder2/folder3/folder4
www.domain.com/folder1/folder2/folder3
www.domain.com/folder1/folder2
www.domain.com/folder1
www.domain.com

1 个答案:

答案 0 :(得分:3)

为什么不使用Uri类?

Dim uri As New Uri("http://www.domain.com/folder1/folder2/folder3/folder4/")

然后,您可以使用Uri.HostUri.Segments

获取所需的所有信息