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

时间:2021-08-21 21:27:54

I wrote a small amount of code to trim a url to last path, until host is reached, but it doesn't work as it should:

我写了一小段代码来修剪一个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)

In my situtation I would like to see 5 box message with url without last path:

在我的位置,我想看到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 个解决方案

#1


3  

Why don't you use Uri class?

你为什么不用Uri课?

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

Then you could get all information you need using Uri.Host and Uri.Segments

然后,您可以使用Uri.Host和Uri.Segments获取所需的所有信息

#1


3  

Why don't you use Uri class?

你为什么不用Uri课?

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

Then you could get all information you need using Uri.Host and Uri.Segments

然后,您可以使用Uri.Host和Uri.Segments获取所需的所有信息