How to trim string(s)/URL(s) to last folder?

flann

Regular Member
Joined
Jan 19, 2008
Messages
208
Reaction score
34
I have this (in a RichTextBox):
Code:
http://www.site.com/folder1/folder2/
http://www.site.com/folder1/folder2/234/
http://www.site.com/folder2/folder2/sagdg
http://www.site.com/folder2/folder4/fdgdf

I need this (i need to remove all crap after 1st directory/folder):
Code:
http://www.site.com/folder1/
http://www.site.com/folder2/

Or maybe someone can tell me how to extract all links on a page with specific classname?
example: <a href="ExtractThisCrap" title="Random Crap" class="ExtractMe"> (i need the ExtractThisCrap ;) i don't get it, maybe it is not working for me because the class is behind the crap/URL i need)
 
Last edited:
find the position of the //

using that as your start, find the position of the first / or the end of the string if there isn't one.

then take that and find either the next / or the end of the string. take everything from there forward.

use the .IndexOf() method off the string class as well as the .Split() method.
 
You could use regex.

Code:
Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        For Each xlink In RichTextBox1.Lines                 ' Replace xx with tt
            Dim MC As MatchCollection = Regex.Matches(xlink, "hxxp://(.*?)/(.*?)/")
            For Each m As Match In MC
                Dim g1 As Group = m.Groups(1)
                Dim g2 As Group = m.Groups(2)
                                    ' Replace xx with tt
                ListBox1.Items.Add("hxxp://" & g1.ToString & "/" & g2.ToString & "/")
            Next
        Next

    End Sub

End Class
 
anyone has a formula for this in excel? looking for one that keeps http or https in place

thanks
 
Back
Top