简体   繁体   中英

What's the best way to compare this in VB.net

I would like to ask you, what your opinion is on the best way to compare URLs. Lets say there are 10 available formats for a URL. I've listed them below.

and some more with slash in the end.

What would be the best solution for easily comparing if these URLs match an item in a listbox. I'm currently doing something else, which constructs 3 different URLs. But the code is too messy, and I'm looking for something a bit cleaner.

I'm looking for a bit something like the code below.

But how well, would this actually compare the two URLs?

For Each result As String In lb_results.Items
   If String.Compare(result, "urls to compare") Then

   End If
Next

if your simply comparing "domain.com" to its variants and need to strip this out...

quickly normalize the string so that the domain name and extension must be between periods.

MessyURL = Replace(MessyURL, "/", ".").Trim

get rid of the last pesky last slash which is now a period if its there.

If Mid(MessyURL, MessyURL.Length, 1) = "." Then
         MessyURL = Mid(MessyURL, 1, MessyURL.Length - 1)

Put the name, a dot, and the com (or whatever) back together.

Dim TestName As String

TestName= MessyURL.Split(".").ElementAt(MessyURL.Split(".").Count - 2) &
 "." & MessyURL.Split(".").ElementAt(MessyURL.Split(".").Count - 1)

and whala, a nice 'domain.com' testname name to compare against a list, and or insert into the list if its not there...

  If MyListOfUrLs.Items.IndexOf(TestName) = -1 then  MyListOfUrLs.Items.Add(TestName)

YOu want just to check if the URLs are the same site use:

For Each result As String In lb_results.Items
   If result.Contains("domain") = True Then
         MsgBox("They have the same URL")
   End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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